无法在Windows上运行babel-node

时间:2017-05-16 17:56:06

标签: node.js express mocha babeljs

如果有人能帮我找出导致下述错误的原因,我将不胜感激。

我在printf("Element size: %zu\n", sizeof(ptr[0]));中有一个scripts部分的测试Express项目,如下所示:

package.json

我正在尝试执行"scripts": { "dev": "backpack", "build": "backpack build", "test-server": "babel-node node_modules/.bin/babel-istanbul cover node_modules/.bin/_mocha", "test-server-ms": "babel-node.cmd node_modules/.bin/babel-istanbul.cmd cover node_modules/.bin/_mocha.cmd" }, 脚本:

test-server-ms

解析babel-node.cmd node_modules/.bin/babel-istanbul.cmd cover node_modules/.bin/_mocha.cmd "test/server" 文件时会产生错误:

babel-istanbul.cmd

遇到(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0\node.exe" ( ^ SyntaxError: Invalid or unexpected token at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:542:28) at Module._extensions..js (module.js:579:10) at Object.require.extensions.(anonymous function) [as .js] (C:\Users\hqrsingh\Development\workspaces\vscode\express-es6-starter\node_modules\babel -register\lib\node.js:152:7) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Function.Module.runMain (module.js:604:10) at Object.<anonymous> (C:\Users\hqrsingh\Development\workspaces\vscode\express-es6-starter\node_modules\babel-cli\lib\_babel-node.js:154:22) 指令时发生错误,该指令是@IF中Windows .cmd脚本的一部分:

node_modules\.bin\babel-istanbul.cmd

我通过@IF EXIST "%~dp0\node.exe" ( "%~dp0\node.exe" "%~dp0\..\babel-istanbul\lib\cli.js" %* ) ELSE ( @SETLOCAL @SET PATHEXT=%PATHEXT:;.JS;=;% node "%~dp0\..\babel-istanbul\lib\cli.js" %* )

在Windows 7 Pro命令shell中解决所有问题
npm

作为参考,这是整个 npm run-script test-server-ms test/server 文件:

package.json

1 个答案:

答案 0 :(得分:0)

FWIW,我能够通过完全避免使用babel-*模块来解决这个问题。我仍然不知道如何通过npm执行特定于平台的脚本,或者甚至应该鼓励这样做。

这是我的整个package.json文件:

{
  "name": "express-es6-starter",
  "version": "2.0.0",
  "description": "A complete boilerplate for Express.js development including unit test and code coverage.",
  "scripts": {
    "start": "node ./build/main.js",
    "clean-build": "rm -rf ./build/*",
    "dev": "backpack dev",
    "build": "backpack build",
    "compile": "babel --presets es2015,stage-0 -d build/ src/",
    "test-client-karma": "node ./node_modules/karma/bin/karma start karma.conf.js || exit 0",
    "test-client-mocha": "mocha test/client/*.js --require babel-register --reporter spec || exit 0",
    "test-server": "mocha --compilers js:babel-core/register test/server/**/*.js",
    "test": "npm run-script test-server; npm run-script test-client-mocha"
  },
  "pre-commit": [
    "test-server"
  ],
  "dependencies": {
    "express": "^4.14.0"
  },
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "backpack-core": "^0.0.7",
    "brfs": "^1.4.3",
    "browserify": "^14.3.0",
    "browserify-shim": "^3.8.14",
    "chai": "^3.5.0",
    "child_process": "^1.0.2",
    "jasmine-core": "^2.6.2",
    "karma": "^1.7.0",
    "karma-chrome-launcher": "^2.1.1",
    "karma-browserify": "^5.1.1",
    "karma-html-reporter": "^0.2.7", 
    "karma-jasmine": "^1.1.0",
    "karma-mocha": "^1.3.0",
    "karma-ng-scenario": "^1.0.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "mocha": "^3.2.0",
    "phantomjs": "^2.1.7",
    "pre-commit": "^1.2.2",
    "webpack": "^1.14.0",
    "watchify": "^3.9.0"
  }
}

这是以上package.json基于的项目结构:

Project Structure