windows下npm scripts不能执行的问题

最近在学webpack为了方便把运行脚本写入package.json文件中,如下:

"scripts": {
    "start": "webpack-dev-server --progress",
    "build": "NODE_ENV=production webpack --config ./webpack.production.config.js --progress"
  }

但是运行npm run build后发现报错,检查了一遍运行脚本并无错误,本人使用的window7环境下的gitbash,本身是支持 linux 命令执行的。感觉应该跟这个有关系,npm-debug.log 报错内容是:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\supfn\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'build' ]
2 info using npm@4.0.3
3 info using node@v6.4.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle webpack_sample_project@1.0.0~prebuild: webpack_sample_project@1.0.0
6 silly lifecycle webpack_sample_project@1.0.0~prebuild: no script for prebuild, continuing
7 info lifecycle webpack_sample_project@1.0.0~build: webpack_sample_project@1.0.0
8 verbose lifecycle webpack_sample_project@1.0.0~build: unsafe-perm in lifecycle true
9 verbose lifecycle webpack_sample_project@1.0.0~build: PATH: C:\Users\supfn\AppData\Roaming\npm\node_modules\npm\bin\node-gyp-bin;C:\webstorm-project\webpack_sample_project\node_modules\.bin;C:\Users\supfn\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\supfn\bin;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Git\cmd;C:\Program Files\Java\jdk1.8.0_102\bin;C:\Program Files\Java\jdk1.8.0_102\jre\bin;C:\Program Files\nodejs;C:\Users\supfn\AppData\Roaming\npm;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
10 verbose lifecycle webpack_sample_project@1.0.0~build: CWD: C:\webstorm-project\webpack_sample_project
11 silly lifecycle webpack_sample_project@1.0.0~build: Args: [ '/d /s /c',
11 silly lifecycle   'NODE_ENV=production webpack --config ./webpack.production.config.js --progress' ]
12 silly lifecycle webpack_sample_project@1.0.0~build: Returned: code: 1  signal: null
13 info lifecycle webpack_sample_project@1.0.0~build: Failed to exec build script
14 verbose stack Error: webpack_sample_project@1.0.0 build: `NODE_ENV=production webpack --config ./webpack.production.config.js --progress`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Users\supfn\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js:279:16)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at EventEmitter.emit (events.js:191:7)
14 verbose stack     at ChildProcess.<anonymous> (C:\Users\supfn\AppData\Roaming\npm\node_modules\npm\lib\utils\spawn.js:40:14)
14 verbose stack     at emitTwo (events.js:106:13)
14 verbose stack     at ChildProcess.emit (events.js:191:7)
14 verbose stack     at maybeClose (internal/child_process.js:852:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
15 verbose pkgid webpack_sample_project@1.0.0
16 verbose cwd C:\webstorm-project\webpack_sample_project
17 error Windows_NT 6.1.7601
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\supfn\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
19 error node v6.4.0
20 error npm  v4.0.3
21 error code ELIFECYCLE
22 error webpack_sample_project@1.0.0 build: `NODE_ENV=production webpack --config ./webpack.production.config.js --progress`
22 error Exit status 1
23 error Failed at the webpack_sample_project@1.0.0 build script 'NODE_ENV=production webpack --config ./webpack.production.config.js --progress'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the webpack_sample_project package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     NODE_ENV=production webpack --config ./webpack.production.config.js --progress
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs webpack_sample_project
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls webpack_sample_project
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

尝试着直接在命令行下执行:

NODE_ENV=production webpack --config ./webpack.production.config.js --progress

是没问题的,但结合到 npm scripts 上,却运行失败。

当我们去掉这个 env 设定,再次执行 npm run build 命令:

"scripts": {
    "start": "webpack-dev-server --progress",
    "build": " webpack --config ./webpack.production.config.js --progress"
  }

就会发现没问题了,但结果当然是不是我想要的,没有设置环境。

怎么办?

想了想可以把要执行的代码写到独立的 JS 中,使用 node 命令来运行,比如:

"scripts": {
    "start": "webpack-dev-server --progress",
    "build": node build.js
  }

build.js 文件中,再设定执行环境之类的:

var ora = require('ora');
var webpack = require('webpack');
var webpackConfig = require('./webpack.production.config');
process.env.NODE_ENV = 'production';

var spinner = ora('building for production...');
spinner.start();

webpack(webpackConfig,function (err, stats) {
   spinner.stop();
    if(err) throw err;
    process.stdout.write(stats.toString({
            colors: true,
            modules: false,
            children: false,
            chunks: false,
            chunkModules: false
        }) + '\n')
});

最后运行 npm run build 结果成功,果然还是最佳实践靠谱一点。
本着多学无害的本心,在网上搜索了一些资料,最后发现一种更简单的方法,直接在package.json里面修改scripts值得内容:

"scripts": {
    "start": "webpack-dev-server --progress",
    "build": "set NODE_ENV=production && webpack --config ./webpack.production.config.js --progress"
  }

这样运行npm run build 也是可以的。

上一篇:Redis简单使用方法说明


下一篇:Open-source Project官方地址