使用Nodemon在Node.js v6.2.0上安装Babel V6.x ES7 Async / Await

男人是一个痛苦的设置! 我已经按照安装说明点击了nodemon框:

https://babeljs.io/docs/setup/#installation

npm install babel-cli babel-preset-es2015 --save-dev 

.babelrc在根目录下:

 { "presets": ["es2015"], "plugins": ["transform-async-to-generator"] } 

package.json (我已经安装了更多的babel的东西,如图所示):

 ... "devDependencies": { "babel-cli": "^6.11.4", "babel-core": "^6.13.2", "babel-plugin-transform-async-to-generator": "^6.8.0", "babel-polyfill": "^6.13.0", "babel-preset-es2015": "^6.13.2", "babel-preset-node6": "^11.0.0", "babel-register": "^6.11.6" }, "scripts": { "startn": "nodemon app.js", "babel-node": "babel-node --presets=es2015 --ignore='foo|bar|baz'", "babel-dev": "nodemon --exec npm run babel-node -- experiment/socketio/test.js" }, ... 

test.js

 (async function () { // <-- error occues here const value = await 123; console.log(value); })().then(() => { console.log('Done'); }); 

我运行命令run-script babel-dev ,如下所示。 错误:

 karl@karl-ux303ln:~/dev/sketch$ npm run-script babel-dev > sketch@0.0.1 babel-dev /home/karl/dev/sketch > nodemon --exec npm run babel-node -- experiment/socketio/test.js [nodemon] 1.10.0 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `npm run babel-node experiment/socketio/test.js` > sketch@0.0.1 babel-node /home/karl/dev/sketch > babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js" /home/karl/dev/sketch/node_modules/babel-core/lib/transformation/file/index.js:591 throw err; ^ SyntaxError: /home/karl/dev/sketch/experiment/socketio/test.js: Unexpected token (1:7) > 1 | (async function () { | ^ 2 | const value = await 123; 3 | console.log(value); 4 | })().then(() => { at Parser.pp.raise (/home/karl/dev/sketch/node_modules/babylon/lib/parser/location.js:22:13) at Parser.pp.unexpected (/home/karl/dev/sketch/node_modules/babylon/lib/parser/util.js:89:8) at Parser.pp.expect (/home/karl/dev/sketch/node_modules/babylon/lib/parser/util.js:83:33) at Parser.pp.parseParenAndDistinguishExpression (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:582:12) at Parser.pp.parseExprAtom (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:481:19) at Parser.pp.parseExprSubscripts (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:277:19) at Parser.pp.parseMaybeUnary (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:257:19) at Parser.pp.parseExprOps (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:188:19) at Parser.pp.parseMaybeConditional (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:165:19) at Parser.pp.parseMaybeAssign (/home/karl/dev/sketch/node_modules/babylon/lib/parser/expression.js:128:19) npm ERR! Linux 3.19.0-65-generic npm ERR! argv "/home/karl/.nvm/versions/node/v6.2.0/bin/node" "/home/karl/.nvm/versions/node/v6.2.0/bin/npm" "run" "babel-node" "experiment/socketio/test.js" npm ERR! node v6.2.0 npm ERR! npm v3.8.9 npm ERR! code ELIFECYCLE npm ERR! sketch@0.0.1 babel-node: `babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the sketch@0.0.1 babel-node script 'babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js"'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the sketch package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! babel-node --presets=es2015 --ignore='foo|bar|baz' "experiment/socketio/test.js" npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs sketch npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls sketch npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! /home/karl/dev/sketch/npm-debug.log [nodemon] app crashed - waiting for file changes before starting... 

我也尝试切换到节点v4.4.7并将npm升级到3.10.6。 还是一样的错误。

       

网上收集的解决方案 "使用Nodemon在Node.js v6.2.0上安装Babel V6.x ES7 Async / Await"

1)删除所有的babel模块(删除所有东西 ,这是我find的一个小错误)

2)安装以下内容:

 npm install --save-dev babel-polyfill babel-preset-es2015 babel-preset-stage-3 babel-register 

3)修复.babelrc文件:

 { "presets": [ "es2015", "stage-3" ] } 

4)检查它是否工作( -r标志是预加载模块):

 node -r babel-register -r babel-polyfill experiment/socketio/test.js 

要修复nodemon:

 nodemon -r babel-register -r babel-polyfill experiment/socketio/test.js 

asynchronous/等待在babeljs中由插件transform-async-to-generator ,它包含在stage-3预置中。 默认情况下,它并不包含在es2015预设中,因此您必须明确添加插件本身或stage-3预设。 要在命令行上执行该操作,请在package.json更改以下行:

 "babel-node": "babel-node --presets=es2015 --ignore='foo|bar|baz'", 

读书:

 "babel-node": "babel-node --presets=es2015,stage-3 --ignore='foo|bar|baz'", 

添加stage-3预设。 同样也可以这样实现:

 "babel-node": "babel-node --presets=es2015 --plugins=transform-async-to-generator --ignore='foo|bar|baz'", 

但是,通常build议使用.babelrcconfiguration文件,这可能很简单:

 { "presets": ["es2015", "stage-3"] } 

要么

 { "presets": ["es2015"], "plugins": ["transform-async-to-generator"] } 

然后package.json的行可以是:

 "babel-node": "babel-node --ignore='foo|bar|baz'", 

(评论上下文的原始答案)

你需要添加:

 "plugins": ["transform-async-to-generator"] 

到你的.babelrc ,因为我不相信async / await包含在任何标准的预设中(因为它可以用多种方式实现)