Webpack npm 脚本找不到命令“src/index.js”

时间:2021-01-07 03:31:30

标签: npm webpack

我在使用 webpack 时遇到了问题,或者至少我认为是使用 webpack。

我已经使用 npm 安装了它。我一直在关注 Mithril.js Simple App 教程,它说在我的 package.json 中执行以下操作

package.json

{
  "name": "my-project",
  "scripts": {
    "start": "webpack src/index.js --output bin/app.js -d --watch"
  }
}

我在项目目录的根目录中运行了以下命令,该目录包含一个 src/ 文件夹,其中包含我的 index.js 文件。我还有一个 bin/ 文件夹,用于保存上面显示的 package.json 文件中指定的 webpack 命令的输出。

npm install webpack webpack-cli

所以我继续运行:

npm start

以下是我在运行时遇到的错误。关于无法找到存在且没有拼错的 src/index.js 的问题,我检查了不止一次。

直接终端输出:

<块引用>
user@ubunutu:~/Projects/mothersrfc/client$ ll
total 60
drwxrwxr-x  2 jreyes jreyes  4096 Jan  6 21:45 bin
drwxrwxr-x 97 jreyes jreyes  4096 Jan  6 21:09 node_modules
-rw-rw-r--  1 jreyes jreyes   382 Jan  6 22:23 package.json
-rw-rw-r--  1 jreyes jreyes 42984 Jan  6 21:09 package-lock.json
drwxrwxr-x  2 jreyes jreyes  4096 Jan  6 22:26 src
user@ubunutu:~/Projects/mothersrfc/client$ npm start
npm WARN lifecycle The node binary used for scripts is /snap/bin/node but npm is using /snap/node/3527/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.

> mothersrfc-client@1.0.0 start /home/jreyes/Projects/mothersrfc/client
> webpack src/index.js --output bin/app.js -d --watch

[webpack-cli] Unknown command 'src/index.js'
[webpack-cli] Run 'webpack --help' to see available commands and options
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! mothersrfc-client@1.0.0 start: `webpack src/index.js --output bin/app.js -d --watch`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the mothersrfc-client@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jreyes/.npm/_logs/2021-01-07T05_00_13_372Z-debug.log
user@ubunutu:~/Projects/mothersrfc/client$ 

这是我来自 npm 的错误日志....

<块引用>
0 info it worked if it ends with ok
1 verbose cli [ '/snap/node/3527/bin/node', '/snap/node/3527/bin/npm', 'start' ]
2 info using npm@6.14.10
3 info using node@v14.15.4
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle mothersrfc-client@1.0.0~prestart: mothersrfc-client@1.0.0
6 info lifecycle mothersrfc-client@1.0.0~start: mothersrfc-client@1.0.0
7 warn lifecycle The node binary used for scripts is /snap/bin/node but npm is using /snap/node/3527/bin/node itself. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
8 verbose lifecycle mothersrfc-client@1.0.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle mothersrfc-client@1.0.0~start: PATH: /snap/node/3527/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/jreyes/Projects/mothersrfc/client/node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
10 verbose lifecycle mothersrfc-client@1.0.0~start: CWD: /home/jreyes/Projects/mothersrfc/client
11 silly lifecycle mothersrfc-client@1.0.0~start: Args: [ '-c', 'webpack src/index.js --output bin/app.js -d --watch' ]
12 silly lifecycle mothersrfc-client@1.0.0~start: Returned: code: 2  signal: null
13 info lifecycle mothersrfc-client@1.0.0~start: Failed to exec start script
14 verbose stack Error: mothersrfc-client@1.0.0 start: `webpack src/index.js --output bin/app.js -d --watch`
14 verbose stack Exit status 2
14 verbose stack     at EventEmitter.<anonymous> (/snap/node/3527/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
14 verbose stack     at EventEmitter.emit (events.js:315:20)
14 verbose stack     at ChildProcess.<anonymous> (/snap/node/3527/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
14 verbose stack     at ChildProcess.emit (events.js:315:20)
14 verbose stack     at maybeClose (internal/child_process.js:1048:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
15 verbose pkgid mothersrfc-client@1.0.0
16 verbose cwd /home/jreyes/Projects/mothersrfc/client
17 verbose Linux 5.4.0-59-generic
18 verbose argv "/snap/node/3527/bin/node" "/snap/node/3527/bin/npm" "start"
19 verbose node v14.15.4
20 verbose npm  v6.14.10
21 error code ELIFECYCLE
22 error errno 2
23 error mothersrfc-client@1.0.0 start: `webpack src/index.js --output bin/app.js -d --watch`
23 error Exit status 2
24 error Failed at the mothersrfc-client@1.0.0 start script.
24 error This is probably not a problem with npm. There is likely additional logging output above.
25 verbose exit [ 2, true ]

编辑:我使用的是 webpack 版本 5.11.1webpack-cli 版本 4.3.1

1 个答案:

答案 0 :(得分:0)

你可以这样做:

package.json

// package.json
{
    "name": "my-project",
    "scripts": {
        //"start": "webpack src/index.js --output bin/app.js -d --watch",
       "build": "webpack --mode=production webpack.config.js",
       "start": "webpack-dev-server --mode=development webpack.config.js"
    }
}

webpack.config.js

const path = require("path")

module.exports = {
   entry: path.join(__dirname, "./src/index.js"),
   output: {
     filename: "[name].bundle.js",
     path: path.resolve(__dirname, "dist")
  },
  ...
}