我正在尝试使用webpack 4学习https://www.youtube.com/watch?v=TzdEpgONurw,the视频之后的webpack,但是我使用webpack 5,但是我下载了webpack-dev-server并启动了npm start,它没有用,我认为这不起作用版本问题,因为我不完全关注视频,所以这是我的调试日志:
matplotlib
这是我的package.json:
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'start'
1 verbose cli ]
2 info using npm@6.14.8
3 info using node@v12.19.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle tut@1.0.0~prestart: tut@1.0.0
6 info lifecycle tut@1.0.0~start: tut@1.0.0
7 verbose lifecycle tut@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle tut@1.0.0~start: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\Walter\Desktop\tut\node_modules\.bin;C:\Python38\Scripts\;C:\Python38\;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\WINDOWS\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Users\Walter\AppData\Roaming\ActiveState\bin;C:\Program Files (x86)\ActiveState Komodo IDE 12\;C:\Users\Walter\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Walter\AppData\Roaming\npm
9 verbose lifecycle tut@1.0.0~start: CWD: C:\Users\Walter\Desktop\tut
10 silly lifecycle tut@1.0.0~start: Args: [ '/d /s /c', 'webpack-dev-server --open' ]
11 silly lifecycle tut@1.0.0~start: Returned: code: 1 signal: null
12 info lifecycle tut@1.0.0~start: Failed to exec start script
13 verbose stack Error: tut@1.0.0 start: `webpack-dev-server --open`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack at EventEmitter.emit (events.js:314:20)
13 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:314:20)
13 verbose stack at maybeClose (internal/child_process.js:1021:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid tut@1.0.0
15 verbose cwd C:\Users\Walter\Desktop\tut
16 verbose Windows_NT 10.0.18363
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v12.19.0
19 verbose npm v6.14.8
20 error code ELIFECYCLE
21 error errno 1
22 error tut@1.0.0 start: `webpack-dev-server --open`
22 error Exit status 1
23 error Failed at the tut@1.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
这是webpack.config.js
{
"name": "tut",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"start": "webpack-dev-server --open"
},
"author": "",
"license": "ISC",
"devDependencies": {
"html-loader": "^1.3.2",
"html-webpack-plugin": "^4.5.0",
"webpack": "^5.1.3",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"http-server": "^0.12.3"
}
}
答案 0 :(得分:2)
尝试将package.json中的start
脚本更改为webpack serve
,如下所示:
"scripts": {
"build": "webpack",
"start": "webpack serve"
}
要启用HMR,请尝试像这样更新devServer
中的webpack.config.js
对象:
devServer: {
contentBase: './dist',
hot: true, // <---- add this line
},