install npm的error指南

在安装npm时遇到问题,报错提示可能由于npm版本与lockfileVersion不匹配或与nodejs版本不适配。解决方法包括:升级或降级npm版本,确保与package-lock.json兼容;若版本不适配,可直接卸载并重新安装指定版本的nodejs,同时删除相关文件,再进行安装并验证版本。

安装npm:

由于需要手机端运行pc上编译的前端进行测试,本小菜鸡要捣鼓npm,结果扑面而来的error~ 以此文章作为记录



npm install遇到的问题

1.Command 'npm' not found, but can be installed with:

输入npm insall之后,报以下提示:

Command 'npm' not found, but can be installed with:

sudo apt install npm

解决方案:

没有安装npm, 输入以下命令进行安装

sudo apt install npm

2.This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!

输入npm insall之后,报以下提示:

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!

> canvas@2.8.0 install /home/root/..../node_modules/canvas
> node-pre-gyp install --fallback-to-build

node-pre-gyp ERR! install zlib: unexpected end of file 
...
...


解决方案:

网上说是因为:当前PC所安装的npm是lockfileVersion@1,而package-lock.json源于lockfileVersion@2。项目中使用的某个包只能用特定版本的npm下载,所以会报错导致npm install失败
升级

npm install -g npm

降级

sudo npm install npm@降级版本号 -g

3.更新了之后结果又开始error

npm install -g npm
npm WARN checkPermissions Missing write access to /usr/local/lib
npm WARN notsup Unsupported engine for npm@9.5.0: wanted: {"node":"^14.17.0 || ^16.13.0 || >=18.0.0"} (current: {"node":"10.19.0","npm":"6.14.4"})
npm WARN notsup Not compatible with your version of node/npm: npm@9.5.0

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/root/.npm/_logs/2023-02-16T05_59_42_512Z-debug.log

4.此时install npm报错又更新了

ERROR: npm v9.5.0 is known not to run on Node.js v10.19.0. You'll need to upgrade
to a newer Node.js version in order to use this version of npm. This version of
npm supports the following node versions: `^14.17.0 || ^16.13.0 || >=18.0.0`. You
can find the latest version at https://nodejs.org/.

ERROR:
/usr/local/lib/node_modules/npm/lib/utils/exit-handler.js:21
  const hasLoadedNpm = npm?.config.loaded
                           ^

SyntaxError: Unexpected token .
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at module.exports (/usr/local/lib/node_modules/npm/lib/cli.js:81:23)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:2:25)
    at Module._compile (internal/modules/cjs/loader.js:778:30)

这个报错的意思是,我的npm的版本不适用在nodejs版本上。查询了一下,说是可以直接安装nodejs,会自动安装对应的npm。
查询版本:

nodejs -v
npm -v
npx -v

查看node安装位置

which node

卸载npm/nodejs:

sudo apt remove npm
sudo apt remove nodejs
sudo apt autoremove

卸载后,还需要删除node相关文件

/usr/local/lib:  node、node_modules 文件夹
/usr/local/include: node 、node_modules 文件夹
/usr/local/bin:node 的可执行文件,如npx、npm

下载指定的nodejs版本:
1.首先要把对应版本的存储库拉下来 [这里可能需要比较久的时间]:

curl -SL https://deb.nodesource.com/setup_16.x | sudo -E bash -

//这里一定要记得敲完命令后,输入root用户的passwoed,不然会一直不动
没输入passwoed

curl -SL https://deb.nodesource.com/setup_16.x | sudo -E bash -
2.安装nodejs

sudo apt -y install nodejs

3.验证nodejs的版本

node  -v
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silversonja

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值