我正在尝试运行命令:
npx webpack
它告诉我它需要webpack-cli并询问是否应该安装它,我说“是”。然后它给了我:
PS C:\_ljdev\webpack demo> npx webpack
npx: installed 321 in 11.89s
One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
- webpack-cli (https://github.com/webpack/webpack-cli)
The original webpack full-featured CLI.
We will use "npm" to install the CLI via "npm install -D".
Do you want to install 'webpack-cli' (yes/no): yes
Installing 'webpack-cli' (running 'npm install -D webpack-cli')...
npm WARN webpack-cli@3.2.3 requires a peer of webpack@4.x.x but none is installed. You must install peer dependencies yourself.
+ webpack-cli@3.2.3
updated 1 package and audited 1053 packages in 2.093s
found 0 vulnerabilities
{ Error: Cannot find module 'webpack-cli'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at runCommand.then (C:\Users\luke.jenner\AppData\Roaming\npm-cache\_npx\3272\node_modules\webpack\bin\webpack.js:143:5)
at process._tickCallback (internal/process/next_tick.js:68:7) code: 'MODULE_NOT_FOUND' }
因此,我尝试通过以下方式手动将其安装在本地:
PS C:\_ljdev\webpack demo> npm install webpack-cli
npm WARN webpack-cli@3.2.3 requires a peer of webpack@4.x.x but none is installed. You must install peer dependencies yourself.
+ webpack-cli@3.2.3
updated 1 package and audited 1053 packages in 8.034s
found 0 vulnerabilities
然后我使用以下方法检查它是否已安装:
PS C:\_ljdev\webpack demo> npm list
webpack-demo@1.0.0 C:\_ljdev\webpack demo
`-- webpack-cli@3.2.3
+-- chalk@2.4.2
| +-- ansi-styles@3.2.1
(other dependencies omitted for brevity)
因此它似乎已安装。
我再次尝试npx webpack并获得完全相同的输出,并提出问题以再次安装webpack-cli。
谁能告诉我为什么找不到webpack-cli本地安装?是否必须全局安装?
或更奇怪的是:为什么它尝试自行安装时会失败?
答案 0 :(得分:1)
我遇到了同样的问题。
经过半天的测试,我终于发现我的项目路径中有特殊字符。删除它们,重新运行npx webpack
,一切正常。
您的项目路径中有一个空格,也许您可以将其删除然后重试。 click here to verify my result
编辑:
对不起,我没有清楚表达。我的意思是在项目路径中有一些特殊字符,这些特殊字符在npm安装期间会转换为其他字符。
如果将工作目录名称(例如,从webpack-demo
更改为webpack/demo
,请删除并重新安装webpack
和webpack-cli
。然后在package.json
目录中打开webpack
包中的node_modules
,您将找到 _where
属性,该属性包含本地绝对路径,但不同于当前的实际项目路径。
我猜(可能不正确,也许还有其他方法),npx
命令将使用_where
属性来定位webpack
包。因此,如果路径错误,npm
将提示您应首先安装webpack-cli
。但是,即使您重新安装了webpack-cli
,其他脚本仍然找不到它。
答案 1 :(得分:0)
尝试全局安装webpack-cli。
True
在github上解决此问题。
npm i -g webpack-cli
答案 2 :(得分:0)
我刚遇到此错误。
删除node_modules
文件夹并用npm i
重新安装依赖项,使npx webpack ...
命令再次起作用。真的不能说为什么...