Javascript 语法错误:在 node.js 中出现意外的标记 const {
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38857215/
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 const { in node.js
提问by Pradeep
I am a new Bie to node.js. I am getting Syntax Error:
我是 node.js 的新手。我收到语法错误:
Unexpected token { in my node.js application
我的 node.js 应用程序中出现意外令牌 {
I am using v4.5.5 on windows 7 64 bit os.
我在 Windows 7 64 位操作系统上使用 v4.5.5。
const {
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
Let me know how to get rid of this.
让我知道如何摆脱这种情况。
Any help would be appreciated.
任何帮助,将不胜感激。
Thanks
谢谢
Pradeep
普拉迪普
回答by Pradeep
The issue got fixed by upgrading node js to v6.3.1
.
After upgrading to 6.3.1
,I got another error related to isomorphoc-fetch that I fixed it by using npm install --save isomorphic-fetch es6-promise
通过将 node js 升级到v6.3.1
. 升级到 后6.3.1
,我遇到了另一个与 isomorphoc-fetch 相关的错误,我通过使用修复了它npm install --save isomorphic-fetch es6-promise
Ref Link:
https://www.npmjs.com/package/isomorphic-fetch.
Ref Link:
https://www.npmjs.com/package/isomorphic-fetch。
Thanks all for your inputs
感谢大家的投入
回答by Risto Novik
From your code const { name, age } = user;
seems like you try using object destruction in Node v4 it's available in Node v6.
从您的代码来看,您const { name, age } = user;
似乎尝试在 Node v4 中使用对象销毁,它在 Node v6 中可用。
If it's not possible to switch to v6 you have to refactor the destruct statements into classical object property access(const name = user.name; const age = user.age
). Also, you could transpile the code with babel.
如果无法切换到 v6,则必须将 destruct 语句重构为经典的对象属性 access( const name = user.name; const age = user.age
)。此外,您可以使用 babel 转译代码。
回答by Jibin TJ
You must probably set harmony destructuring flag like, (if you are using less than Node version 6)
您可能必须设置和谐解构标志,例如(如果您使用的版本低于 Node 6)
node --harmony-destructuring app.js