在centos7下执行 npm install 和 npm install webpack --save-dev 都报了如下错,很郁闷,搞了好久。
npm WARN ajv-errors@1.0.1 requires a peer of ajv@>=5.0.0 but none is
installed. You must install peer dependencies yourself.npm ERR! code ENOENT npm ERR! syscall access npm ERR! path
/yonyou/uat-workspace/occ-project/hhocc/occ-portal-static/node_modules/_accepts@1.3.7@accepts/node_modules/mime-types
npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory,
access
‘/yonyou/uat-workspace/occ-project/hhocc/occ-portal-static/node_modules/_accepts@1.3.7@accepts/node_modules/mime-types’
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoentnpm ERR! A complete log of this run can be found in: npm ERR!
/root/.npm/_logs/2020-04-15T03_37_37_044Z-debug.log
[root@ecs-639d-0002 occ-portal-static]# npm run webpack npm ERR!
missing script: webpacknpm ERR! A complete log of this run can be found in: npm ERR!
/root/.npm/_logs/2020-04-15T03_38_08_854Z-debug.log
最终解决是使用 rm -rf 命令 删除项目下的 node_modules 文件
即:rm -rf node_modules/
如图:
再执行: npm install webpack --save-dev
,如图:
有些环境中执行 npm install webpack --save-dev
可能会报如下错误:
[root@b-0002 occ-portal-react]# npm install webpack --save-dev
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: occ-portal-shop@2.0.0
npm ERR! Found: react@16.8.6
npm ERR! node_modules/react
npm ERR! react@"^16.8.6" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"0.12 - 16.2" from qrcode-react@0.1.16
npm ERR! node_modules/qrcode-react
npm ERR! qrcode-react@"0.1.16" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See /root/.npm/eresolve-report.txt for a full report.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-11-26T12_50_36_916Z-debug.log
[root@b-0002 occ-portal-react]# ll
total 2316
原因
ERESOLVE与npm@7有关的问题很常见,因为npm7.x对某些事情比npm6.x更严格。通常,最简单的解决方法是将–legacy-peer-deps标志传递给npm(e.g.,npm i --legacy-peer-deps),或者使用npm@6。
如果这不能立即起作用,也许可以先删除node_modules和package-lock.json。它们将被重新创建。
解决方法
降级npm到6版本
或
使用npx指定npm的版本
npx -p npm@6 npm i --legacy-peer-deps
本人是删除node_modules和package-lock.json后,重新执行 npm install webpack --save-dev 就可以了。
执行成功后再执行:npm install
,如图: