npm 运行 dev 无法安装 webpack cli

npm run dev can not install webpack cli

我有一个 laravel 应用程序:

当我想要 运行 npm run dev 时,我有这个 问题 :

    CLI for webpack must be installed.
    webpack-cli (https://github.com/webpack/webpack-cli)
    Do you want to install 'webpack-cli' (yes/no):

输入 yes 后我有这个 错误 :

    Error: Cannot find module 'webpack-cli/package.json'
    Require stack:
    - C:\Users\SibCo\AppData\Roaming\npm-cache\_npx52\node_modules\webpack\bin\webpack.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.resolve (internal/modules/cjs/helpers.js:78:19)
    at runCli (C:\Users\SibCo\AppData\Roaming\npm-cache\_npx52\node_modules\webpack\bin\webpack.js:50:26)
    at C:\Users\SibCo\AppData\Roaming\npm-cache\_npx52\node_modules\webpack\bin\webpack.js:139:5
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    'C:\Users\SibCo\AppData\Roaming\npm-cache\_npx\5852\node_modules\webpack\bin\webpack.js'
     ]
    }

尝试全局安装 webpack:

npm install -g webpack webpack-cli

如果您使用的是 webpack-cli 4webpack 5,请将 webpack-dev-server 更改为 webpack serve

示例:

"serve": "webpack serve --config config/webpack.dev.js --progress"

您可能还想查看 GitHub 上的评论:

NPM package.json 脚本是 运行 本地安装的二进制文件的一种方便而有用的方法,无需关心它们的完整路径。简单地定义一个脚本:

对于 webpack-cli 3.x:

"scripts": { "start:dev": "webpack-dev-server" }

对于 webpack-cli 4.x:

"scripts": { "start:dev": "webpack serve" }

更新 我的 npm 版本到 v7.11.2 并删除 node_modules 和催促:

    npm install

再次,它对我有用。