我的React应用可以在开发模式(npm run build
)中执行webpack.config.dev.js
,但不能在生产模式(webpack.config.prod.js
)中执行。抛出以下错误:
Module parse failed: ...\node_modules\npm\bin\npm-cli.js Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
at Parser.pp$4.raise (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:2221:15)
at Parser.pp$7.getTokenFromCode (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:2756:10)
at Parser.pp$7.readToken (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:2477:17)
at Parser.pp$7.nextToken (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:2468:15)
at Parser.parse (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:515:10)
at Object.parse (...\node_modules\webpack\node_modules\acorn\dist\acorn.js:3098:39)
at Parser.parse (...\node_modules\webpack\lib\Parser.js:902:15)
at NormalModule.<anonymous> (...\node_modules\webpack\lib\NormalModule.js:104:16)
at NormalModule.onModuleBuild (...\node_modules\webpack-core\lib\NormalModuleMixin.js:310:10)
at nextLoader (...\node_modules\webpack-core\lib\NormalModuleMixin.js:275:25)
at ...\node_modules\webpack-core\lib\NormalModuleMixin.js:259:5
at Storage.finished (...\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:38:16)
at ...\node_modules\graceful-fs\graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
npm info lifecycle app@0.1.0~build: Failed to exec build script
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@0.1.0 build: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app@0.1.0 build 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! ...\_logs\2018-03-09T15_48_06_442Z-debug.log
我一直在搜索非常多,并为Unexpected character '#'
问题找到多个解决方案,但实际上并没有为我做好任何事情。问题这是npm-cli.js
被捆绑,第一行包含#!/usr/bin/env node
。 如何才能最好地解决此问题?
BannerPlugin.js
(docs):引发了here所描述的新问题,并未实际解决原始问题。 Source (StackOverflow)。shebang-loader
:有不同的方法可以导入这个加载器,我尝试了很多,但似乎没有一个实际可行。最重要的是,最后一次更新是在2年前,并且没有明确的文档(我从GitHub线程获得了大部分信息链接到此repo或来自他们的问题)Source (GitHub)。webpack@1.14.0
” ...\_logs\2018-03-09T15_48_06_442Z-debug.log
0 info it worked if it ends with ok
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 'run',
1 verbose cli 'build' ]
2 info using npm@5.6.0
3 info using node@v8.9.4
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle app@0.1.0~prebuild: app@0.1.0
6 verbose lifecycle app@0.1.0~prebuild: unsafe-perm in lifecycle true
7 verbose lifecycle app@0.1.0~prebuild: PATH: ALLPATHSFROMPC
8 verbose lifecycle app@0.1.0~prebuild: CWD: PROJECTPATH
9 silly lifecycle app@0.1.0~prebuild: Args: [ '/d /s /c', 'rimraf ./build' ]
10 silly lifecycle app@0.1.0~prebuild: Returned: code: 0 signal: null
11 info lifecycle app@0.1.0~build: app@0.1.0
12 verbose lifecycle app@0.1.0~build: unsafe-perm in lifecycle true
13 verbose lifecycle app@0.1.0~build: PATH: ALLPATHSFROMPC
14 verbose lifecycle app@0.1.0~build: CWD: PROJECTPATH
15 silly lifecycle app@0.1.0~build: Args: [ '/d /s /c', 'node scripts/build.js' ]
16 silly lifecycle app@0.1.0~build: Returned: code: 1 signal: null
17 info lifecycle app@0.1.0~build: Failed to exec build script
18 verbose stack Error: app@0.1.0 build: `node scripts/build.js`
18 verbose stack Exit status 1
18 verbose stack at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
18 verbose stack at emitTwo (events.js:126:13)
18 verbose stack at EventEmitter.emit (events.js:214:7)
18 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
18 verbose stack at emitTwo (events.js:126:13)
18 verbose stack at ChildProcess.emit (events.js:214:7)
18 verbose stack at maybeClose (internal/child_process.js:925:16)
18 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
19 verbose pkgid app@0.1.0
20 verbose cwd PROJECTPATH
21 verbose Windows_NT 10.0.16299
22 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
23 verbose node v8.9.4
24 verbose npm v5.6.0
25 error code ELIFECYCLE
26 error errno 1
27 error app@0.1.0 build: `node scripts/build.js`
27 error Exit status 1
28 error Failed at the app@0.1.0 build script.
28 error This is probably not a problem with npm. There is likely additional logging output above.
29 verbose exit [ 1, true ]
备注:
app
”PROJECTPATH
”或“...
”ALLPATHSFROMPC
”编辑:
2018-03-09T15_48_06_442Z-debug.log
部分build.js
文件添加为Pastebin链接package.json
文件添加为Pastebin链接答案 0 :(得分:4)
经过调查,我发现问题来自package.json。
1)从package.json中删除npm和npm-run-all模块:
"npm": "^5.7.1",
"npm-run-all": "^4.0.2",
2)从webpack.config.prod.js中删除BannerPlugin
// version 1 webpack
new webpack.BannerPlugin("#!/usr/bin/env node", { raw: false }),
// version 2 webpack
new webpack.BannerPlugin({
banner: "#!/usr/bin/env node",
raw: true
});
我们不再需要这些了。
3)使用稳定的npm-和node版本进行全局执行:
请参阅此webpack 2 sample(克隆并在本地尝试)并运行npm run coba
。
问题出在package.json中。当webpack在生产模式下构建时,它会认为需要包含npm和npm-run-all模块,而不是它们。
您不应将package.json中的npm包含为模块,因为它是包管理器可执行文件。