NPM 启动:脚本的无效值错误 (windows)

NPM Start: INVALID VALUES error for SCRIPTS (windows)

在 Windows 机器上使用 IntelliJ 尝试 NPM 启动 React 应用程序时遇到问题。该应用程序基于 Mac 构建,似乎可以毫无问题地从 MAC 开始。这可能与使用 Windows 机器时脚本变量的格式有关?

调试日志:

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   'start' ]
2 info using npm@6.9.0
3 info using node@v10.16.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle recorderApp@1.0.0~prestart: recorderApp@1.0.0
6 info lifecycle recorderApp@1.0.0~start: recorderApp@1.0.0
7 verbose lifecycle recorderApp@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle recorderApp@1.0.0~start: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\brendan\cst-Compozed\call-recorder\client\node_modules\.bin;C:\ProgramData\Boxstarter;c:\oracle\product.1.0.2\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;c:\oracle\product.1.0.2_32b\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\PuTTY\;C:\Windows\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Amazon\AWSCLI\;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_201\bin;C:\Program Files\Java\jre1.8.0_201\bin;C:\Program Files\dotnet\;C:\Users\brendan\AppData\Local\Microsoft\WindowsApps;C:\Users\brendan\.dotnet\tools;C:\Program Files\Java\jdk1.8.0_212\bin;C:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_221\bin;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\Apache Maven\apache-maven-3.6.2\bin;;C:\Program Files\Java\jdk1.8.0_231\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Users\brendan\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\brendan\AppData\Local\Programs\Python\Python36-32\;C:\Users\brendan\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jre1.8.0_171\bin;C:\Gradle\gradle-5.2.1\bin;C:\Users\brendan\AppData\Local\JetBrains\IntelliJ IDEA Community Edition 2018.3.5\bin;;C:\Users\brendan\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\brendan\AppData\Roaming\npm;C:\Program Files\Apache Maven\apache-maven-3.6.2\bin;;C:\Users\brendan\AppData\Local\Microsoft\WindowsApps
9 verbose lifecycle recorderApp@1.0.0~start: CWD: C:\Users\brendan\cst-Compozed\call-recorder\client
10 silly lifecycle recorderApp@1.0.0~start: Args: [ '/d /s /c',
10 silly lifecycle   'webpack --mode none; NODE_ENV=local webpack-dev-server --host localhost --port 3000' ]
11 silly lifecycle recorderApp@1.0.0~start: Returned: code: 1  signal: null
12 info lifecycle recorderApp@1.0.0~start: Failed to exec start script
13 verbose stack Error: recorderApp@1.0.0 start: `webpack --mode none; NODE_ENV=local webpack-dev-server --host localhost --port 3000`
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:301:16)
13 verbose stack     at EventEmitter.emit (events.js:198:13)
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:198:13)
13 verbose stack     at maybeClose (internal/child_process.js:982:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid recorderApp@1.0.0
15 verbose cwd C:\Users\brendan\cst-Compozed\call-recorder\client
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
18 verbose node v10.16.0
19 verbose npm  v6.9.0
20 error code ELIFECYCLE
21 error errno 1
22 error recorderApp@1.0.0 start: `webpack --mode none; NODE_ENV=local webpack-dev-server --host localhost --port 3000`
22 error Exit status 1
23 error Failed at the recorderApp@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 ]

PACKAGE.JSON

{
  "name": "recorderApp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "eslint": "eslint src/**/**/**/*.js --fix && eslint src/**/**/*.js --fix",
    "start": "webpack --mode none; NODE_ENV=local webpack-dev-server --host localhost --port 3000",
    "webpack": "./node_modules/.bin/webpack --mode development --config webpack.config.js"
  },

windows 失败的原因是在脚本中设置了 NODE_ENV 个变量,*inxwindows 不兼容。

cross-env可以解决这个问题。查看文档 here

使用示例

start: "cross-env NODE_ENV=production"