Javascript SyntaxError:意外的令牌函数 - Async Await Nodejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37815790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: Unexpected token function - Async Await Nodejs
提问by bozzmob
I was experimenting on using Node version 6.2.1with some of my code. Had plans to migrate most of the hyper-callback oriented codes to something that looks cleaner and maybe performs better.
我正在尝试将 Node 6.2.1版与我的一些代码一起使用。曾计划将大多数面向超级回调的代码迁移到看起来更干净并且性能可能更好的东西。
I have no clue why, the terminal throws up an error when I try to execute the node code.
我不知道为什么,当我尝试执行节点代码时终端会抛出错误。
helloz.js
hello.js
(async function testingAsyncAwait() {
await console.log("Print me!");
})();
Logs-
日志-
BOZZMOB-M-T0HZ:rest bozzmob$ node helloz.js
/Users/bozzmob/Documents/work/nextgennms/rest/helloz.js:1
(function (exports, require, module, __filename, __dirname) { (async function testingAsyncAwait() {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3
BOZZMOB-M-T0HZ:rest bozzmob$ node -v
v6.2.1
What am I missing? Please throw me some light on the same.
我错过了什么?请给我一些启发。
Update 1:
更新 1:
I tried to use Babel as Quentin suggested, But, I am getting the following error still.
我尝试按照 Quentin 的建议使用 Babel,但是,我仍然收到以下错误。
Updated Code-
更新代码-
require("babel-core/register");
require("babel-polyfill");
(async function testingAsyncAwait() {
await console.log("Print me!");
})();
Logs-
日志-
BOZZMOB-M-T0HZ:rest bozzmob$ babel helloz.js > helloz.trans.js
SyntaxError: helloz.js: Unexpected token (3:7)
1 | require("babel-polyfill");
2 |
> 3 | (async function testingAsyncAwait() {
| ^
4 | await console.log("Print me!");
5 | })();
回答by Quentin
Async functions are not supported by Node versions older than version 7.6.
低于 7.6 的 Node 版本不支持异步函数。
You'll need to transpile your code (e.g. using Babel) to a version of JS that Node understands if you are using an older version.
如果您使用的是旧版本,您需要将您的代码(例如使用Babel)转换为 Node 能够理解的 JS 版本。
That said, the current (2018) LTSversion of Node.js is 8.x, so if you are using an earlier version you should very strongly consider upgrading.
也就是说,Node.js的当前 (2018) LTS版本是 8.x,因此如果您使用的是早期版本,则应强烈考虑升级。
回答by Phillipe Moreira
Nodejs supports async/await from version 7.6.
Nodejs 从 7.6 版开始支持 async/await。
Release post: https://v8project.blogspot.com.br/2016/10/v8-release-55.html
发布帖:https: //v8project.blogspot.com.br/2016/10/v8-release-55.html
回答by Nivesh
Node.JS does not fully support ES6 currently, so you can either use asyncawaitmodule or transpileit using Bable.
Node.JS 目前不完全支持 ES6,所以你可以使用asyncawait模块或使用Bable转译它。
install
安装
npm install --save asyncawait
helloz.js
hello.js
var async = require('asyncawait/async');
var await = require('asyncawait/await');
(async (function testingAsyncAwait() {
await (console.log("Print me!"));
}))();
回答by stujo
If you are just experimenting you can use babel-node
command line tool to try out the new JavaScript features
如果您只是在试验,您可以使用babel-node
命令行工具来尝试新的 JavaScript 功能
Install
babel-cli
into your project$ npm install --save-dev babel-cli
Install the presets
$ npm install --save-dev babel-preset-es2015 babel-preset-es2017
Setup your babel presets
Create
.babelrc
in the project root folder with the following contents:{ "presets": ["es2015","es2017"] }
Run your script with
babel-node
$ babel-node helloz.js
安装
babel-cli
到你的项目中$ npm install --save-dev babel-cli
安装预设
$ npm install --save-dev babel-preset-es2015 babel-preset-es2017
设置你的 babel 预设
.babelrc
在项目根文件夹中创建,内容如下:{ "presets": ["es2015","es2017"] }
运行你的脚本
babel-node
$ babel-node helloz.js
This is only for development and testing but that seems to be what you are doing. In the end you'll want to set up webpack (or something similar) to transpile all your code for production
这仅用于开发和测试,但这似乎是您正在做的。最后你会想要设置 webpack(或类似的东西)来转译你所有的生产代码
- babel-node sample code : https://github.com/stujo/javascript-async-await/tree/15abac
- babel-node 示例代码:https: //github.com/stujo/javascript-async-await/tree/15abac
If you want to run the code somewhere else, webpack can help and here is the simplest configuration I could work out:
如果你想在其他地方运行代码,webpack 可以提供帮助,这是我能解决的最简单的配置:
- Full webpack example : https://github.com/stujo/javascript-async-await
- 完整的 webpack 示例:https: //github.com/stujo/javascript-async-await
回答by slideshowp2
node v6.6.0
node v6.6.0
If you just use in development. You can do this:
如果你只是在开发中使用。你可以这样做:
npm i babel-cli babel-plugin-transform-async-to-generator babel-polyfill --save-dev
the package.json
would be like this:
该package.json
会是这样:
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-plugin-transform-async-to-generator": "^6.16.0",
"babel-polyfill": "^6.20.0"
}
create .babelrc
file and write this:
创建.babelrc
文件并写入:
{
"plugins": ["transform-async-to-generator"]
}
and then, run your async/await
script like this:
然后,async/await
像这样运行你的脚本:
./node_modules/.bin/babel-node script.js
回答by Theophilus Omoregbee
Though I'm coming in late, what worked for me was to install transform-async-generatorand transform-runtimeplugin like so:
虽然我来晚了,但对我有用的是安装transform-async-generator和transform-runtime插件,如下所示:
npm i babel-plugin-transform-async-to-generator babel-plugin-transform-runtime --save-dev
npm i babel-plugin-transform-async-to-generator babel-plugin-transform-runtime --save-dev
the package.json
would be like this:
该package.json
会是这样:
"devDependencies": {
"babel-plugin-transform-async-to-generator": "6.24.1",
"babel-plugin-transform-runtime": "6.23.0"
}
create .babelrc
file and write this:
创建.babelrc
文件并写入:
{
"plugins": ["transform-async-to-generator",
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
]
}
and then happy coding with async/await
然后愉快地编码 async/await
回答by Githithu Wambura
include and specify the node engine version to the latest, say at this time I did add version 8.
包含并指定最新的节点引擎版本,比如我此时确实添加了版本 8。
{
"name": "functions",
"dependencies": {
"firebase-admin": "~7.3.0",
"firebase-functions": "^2.2.1",
},
"engines": {
"node": "8"
},
"private": true
}
in the following file
在以下文件中
package.json
包.json