npm-scripts 在 windows 下的兼容问题

今天有开辟的同事问我说,在本机开辟的 windows 上运转构建敕令报错,不知道怎样回事儿。
作为一位 Mac 用户,固然也不知道为何啦,过去看看,果真有毛病。

我们的开辟构建敕令是基于 npmscripts,实在就是很简单的一句话:

{
  "scripts": {
    ...
    "build": "NODE_ENV=production webpack --progress --hide-modules"
  },
  ...
}

这个敕令在 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   'run',
1 verbose cli   'build' ]
2 info using npm@3.10.8
3 info using node@v6.9.1
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle @~prebuild: @
6 silly lifecycle @~prebuild: no script for prebuild, continuing
7 info lifecycle @~build: @
8 verbose lifecycle @~build: unsafe-perm in lifecycle true
9 verbose lifecycle @~build: PATH: C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;<WORK_DIR>\node_modules\.bin;C:\Users\<USER_DIR>\bin;F:\Git\mingw64\bin;F:\Git\usr\local\bin;F:\Git\usr\bin;F:\Git\usr\bin;F:\Git\mingw64\bin;F:\Git\usr\bin;C:\Users\<USER_DIR>\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\nodejs;C:\Go\bin;C:\Users\<USER_DIR>\AppData\Local\Microsoft\WindowsApps;C:\Users\<USER_DIR>\AppData\Roaming\npm;F:\Git\usr\bin\vendor_perl;F:\Git\usr\bin\core_perl
10 verbose lifecycle @~build: CWD: <WORK_DIR>
11 silly lifecycle @~build: Args: [ '/d /s /c',
11 silly lifecycle   'NODE_ENV=production webpack --progress --hide-modules' ]
12 silly lifecycle @~build: Returned: code: 1  signal: null
13 info lifecycle @~build: Failed to exec build script
14 verbose stack Error: @ build: `NODE_ENV=production webpack --progress --hide-modules`
14 verbose stack Exit status 1
14 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js:255: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:\Program Files\nodejs\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:877:16)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
15 verbose pkgid @
16 verbose cwd <WORK_DIR>
17 error Windows_NT 10.0.14393
18 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
19 error node v6.9.1
20 error npm  v3.10.8
21 error code ELIFECYCLE
22 error @ build: `NODE_ENV=production webpack --progress --hide-modules`
22 error Exit status 1
23 error Failed at the @ build script 'NODE_ENV=production webpack --progress --hide-modules'.
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  package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error     NODE_ENV=production webpack --progress --hide-modules
23 error You can get information on how to open an issue for this project with:
23 error     npm bugs
23 error Or if that isn't available, you can get their info via:
23 error     npm owner ls
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

调试了一下,题目就出在这个敕令自身。

这位同砚开辟用的是 git-shell 自带的敕令行,自身是支撑 linux 敕令实行的。
比方我们直接在敕令行下实行:

NODE_ENV=production
export NODE_ENV=production

都是没题目的,但结合到 npm scripts 上,却运转失利。
当我们去掉这个 env 设定,再次实行 npm scripts 敕令:

{
  "scripts": {
    ...
    "build": "webpack --progress --hide-modules"
  },
  ...
}

就会发明没题目了,但效果固然是否是我们想要的。

怎样办?

能够把要实行的代码写到自力的 JS 中,运用 node 敕令来运转,比方:

{
  "scripts": {
    ...
    "build": "node build/build.js"
  },
  ...
}

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

var ora = require('ora')
var webpack = require('webpack')
var webpackConfig = require('../webpack.config.js')

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')
})

这实在也是 vue 脚手架自动天生的构建体式格局,果真照样最好实践靠谱一点。

    原文作者:micate
    原文地址: https://segmentfault.com/a/1190000007709007
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞