• Welcome to Journal web site.

我是 PHP 程序员

- 开发无止境 -

Next
Prev

npm install 常见报错与解决方案

Data: 2022-04-23 11:40:08Form: JournalClick: 9

1.

mpm ERR! Unexpected end of JSON input while parsing near ...
npm ERR! errno -4048
npm ERR! syscall scandir
  • 解决方案
npm cache clean --force
npm install

 

2.

npm ERR! cb() never called!
npm ERR! This is an error with npm itself.
  • 解决方案
1. 删除npm文件夹下面的node_modules
2. 删除当前项目文件夹的node_modules
3. 删除package-lock.json文件
4. npm cache clean --force
5. npm install

 

3.

npm ERR! chromedriver@2.46.0 install: `node install.js`
npm ERR! Exit status 1
npm ERR! deasync@0.1.21 install: `node ./build.js`
npm ERR! Exit status 1
npm ERR! phantomjs-prebuilt@2.1.13 install: `node install.js`
npm ERR! Exit status 1
  • 解决方案

类似所有的执行 node 命令的报错,基本都可以用这个方法解决#,--ignore-scripts 是一个选项,它告诉 npm 在安装模块时不要执行任何脚本。通常,模块的作者会在其 package.json 文件中定义一些脚本,这些脚本在安装、卸载或其他生命周期事件发生时执行。使用 --ignore-scripts 选项可以避免执行这些脚本,可以避免安装错误!

npm install --ignore-scripts
npm install deasync@0.1.21 --ignore-scripts

 

4.

Cannot download 'xxxx'
HTTP error 404 Not Found
  • 解决方案

默认下载路径上无法找到对应的资源文件,直接手动修改包的下载地址即可#

sass_binary_site = https://npm.taobao.org/mirrors/node-sass
phantomjs_cdnurl = http://10.25.220.15:8080

 

5.

code ERESOLVE 
npm ERR! ERESOLVE unable to resolve dependency tree
  • 解决方案

--legacy-peer-deps 选项告诉 npm 使用“旧版” peerDependencies 检查,即使用较宽松的规则,而不是默认的更严格规则。
在 npm 7.x 版本及以上,npm 引入了更严格的 peerDependencies 检查机制,以确保模块之间的兼容性和正确性。然而,有时这会导致一些旧的或不再维护的模块与新的依赖关系规则不兼容。
需要注意的是,使用 --legacy-peer-deps 可能会导致一些潜在的兼容性问题,因此建议尽量避免使用这个选项,而是尝试更新或替代模块,以使其符合最新的依赖关系规范。

npm i --legacy-peer-deps

ERESOLVE相关的报错原因大多也确实是npm7与npm6之间的差异所导致的。
当然你也可以选择降版本到npm6来解决。


6.

Error: error:0308010C:digital envelope routines::unsupported
  • 解决方案

这个错误信息指的是 OpenSSL 的一个错误, OpenSSL 的数字信封操作中使用了不受支持的算法或者操作。这可能是因为 OpenSSL 版本不同,或者一些特定的配置问题。
我们可以指定 Node.js 使用旧版的 OpenSSL 提供程序。


或遇到此类未知问题也可以尝试这个方法

npm ERR! code ELIFECYCLE
npm ERR! errno 1

 

Windows

set NODE_OPTIONS=--openssl-legacy-provider

Unix (Linux, macOS, Git bash等)

export NODE_OPTIONS=--openssl-legacy-provider

PowerShell

$env:NODE_OPTIONS = "--openssl-legacy-provider"

另外一个方法是在项目的package.json文件里将

"start": "react-scripts start"
替换成:
"start": "react-scripts --openssl-legacy-provider start"

 

7.

code CERT_HAS_EXPIRED
  • 解决方案

表示你正在尝试访问的服务器的SSL证书已经过期。

清除npm缓存
 npm cache clean --force
取消ssl验证:
 npm config set strict-ssl false
之后再npm install 你想安装的东西

再不行的话试试更换npm镜像源:

npm config set registry http://registry.cnpmjs.org
npm config set registry http://registry.npm.taobao.org 

 

8.

 peer eslint@">= 1.6.0 < 7.0.0" from @vue/cli-plugin-eslint@4.5.15
  • 解决方案

提示依赖包对版本有要求,不能太高也不能太低

手动指定安装依赖(降低版本到6.xx)
 npm install eslint@6
如还在保存尝试降低npm版本(-g全局安装npm版本到6.xx)
 npm install -g npm@6

 

Name:
<提交>