最近在学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 也是可以的。

windows下npm scripts不能执行的问题的更多相关文章

  1. windows下编写shell脚本执行错误

    在 windows 下,换行符是 \r\n,在linux下,换行符是 \n.如果你在IDEA里写sh脚本,可以手动设置脚本的换行符为 \n,如果你用notepad++写脚本,可以显示所有字符,以便明确 ...

  2. 【微信Java开发 --1---番外1】在windows下,使用JAVA执行多条DOS命令+文件夹/路径中有空格怎么解决【目的是实现内容穿透外网】

    内网穿透外网的那一篇,参正集1 但是每次都要Ctrl+R 启动DOS窗口,也就是CMD,一句一句的去粘,略显繁琐. 所以将这些任务写在JAVA程序中,启动一次程序就可以实现[内网穿透]的功能,多好啊! ...

  3. 环信webim1.1.2版本在windows下npm环境搭建错误解决

    1.1.2版本的webim从ui到整体的代码结构都做了很大改变,从代码结构上采用node.js的环境进行开发和打包,最终打包的输出项目,不依赖node.js的环境进行运行,得益于webpack的打包实 ...

  4. windows下npm安装vue

    一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...

  5. Windows 下Npm和NodeJS升级

    前提电脑中已经安装过NodeJS, npm.现在需要进行升级操作. 1.查看当前的npm和NodeJs的版本: C:\Users\Administrator>node -vv4.4.3 C:\U ...

  6. windows下使用Eclipse编译执行MapReduce程序 Hadoop2.6.0/Ubuntu

    一.环境介绍 宿主机:windows8 虚拟机:Ubuntu14.04 hadoop2.6伪分布:搭建教程http://blog.csdn.net/gamer_gyt/article/details/ ...

  7. Windows下直接双击可执行的jar

    如果没有设置,那么就是用命令行: jar处在文件夹路径下打开命令行:java -jar xxx.jar 总的来说是有点不方便 首先默认打开jar程序得是相同jdk的java.exe 然后是一闪而过 下 ...

  8. windows下npm和node如何升级

    1.npm升级 访问官网:npm 可以看到如下图: 就是如果你要更新你的版本,请在终端输入以下语句: npm install npm@latest -g 效果如下图:  2.node升级 直接在nod ...

  9. windows下npm默认的全局路径

    C:\Users\用户名\AppData\Roaming\npm\node_modules

随机推荐

  1. POJ 3602 Typographical Ligatures

    [题意简述]:题意就是输入一串字符串,问我们有多少种不同的字符,也就是说出现过一次的字符,下次就不记到种数中了,特别的有  ff, fi ,fl ,ffi ,ffl,'',``, 这几个每一个算是一种 ...

  2. Sql开发技巧

    原文:Sql开发技巧 简介 本文主要介绍下述几个技巧: 使用Row_Number分页 事务 根据条件刷选记录的技巧 分页 主要是使用了Row_Number()这个函数.一般如下: declare @P ...

  3. jQuery无限级联下拉框插件

    自己编写jQuery插件 之 无限级联下拉框   因为是级联,所以数据必须是树型结构的,我这里的测试数据如下: 看下效果图: 1.>图一: 2.>图二: 3.>图三: 由图可知,下拉 ...

  4. js实现在新标签页打开页面

    这种方法打开新标签页要在IE9+以上浏览器才可以! <html> <head> <meta http-equiv="Content-type" con ...

  5. 探索Android该Parcel机制上

    一.先从Serialize说起 我们都知道JAVA中的Serialize机制.译成串行化.序列化……,其作用是能将数据对象存入字节流其中,在须要时又一次生成对象.主要应用是利用外部存储设备保存对象状态 ...

  6. leetcode[94] Unique Binary Search Trees

    给定n,那么从1,2,3...n总共可以构成多少种二叉查找数呢.例如给定3 Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ ...

  7. using和yield return

    C#中的using和yield return混合使用 最近写代码为了为了省事儿用了几个yield return,因为我不想New一个List<T>或者T[]对象再往里放元素,就直接返回IE ...

  8. Amazon前技术副总裁解剖完美技术面试

    Amazon前技术副总裁解剖完美技术面试 投递人 itwriter 发布于 2014-03-03 14:30 评论(0) 有1729人阅读  原文链接  [收藏]  « » 英文原文:The Anat ...

  9. 最短路径 dijkstra

    最短路径 dijkstra #include <stdio.h> #include <string.h> #include <limits.h> #define M ...

  10. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...