Webpack npm 脚本找不到命令“src/index.js”
2021-01-07
2761
我在使用 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. > [email protected] 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! [email protected] start: `webpack src/index.js --output bin/app.js -d --watch` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the [email protected] 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 [email protected] 3 info using [email protected] 4 verbose run-script [ 'prestart', 'start', 'poststart' ] 5 info lifecycle [email protected]~prestart: [email protected] 6 info lifecycle [email protected]~start: [email protected] 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 [email protected]~start: unsafe-perm in lifecycle true 9 verbose lifecycle [email protected]~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 [email protected]~start: CWD: /home/jreyes/Projects/mothersrfc/client 11 silly lifecycle [email protected]~start: Args: [ '-c', 'webpack src/index.js --output bin/app.js -d --watch' ] 12 silly lifecycle [email protected]~start: Returned: code: 2 signal: null 13 info lifecycle [email protected]~start: Failed to exec start script 14 verbose stack Error: [email protected] 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 [email protected] 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 [email protected] start: `webpack src/index.js --output bin/app.js -d --watch` 23 error Exit status 2 24 error Failed at the [email protected] 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.1
和
webpack-cli
版本
4.3.1
2个回答
你可以这样做:
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")
},
...
}
Kenedy Nopriansyah
2021-01-07
webpack-cli v4 改变了入口点的解析方式。
v3
webpack a.js b.js -o dist
v4
webpack --entry a.js --entry b.js -o dist
fregante
2021-01-15