Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected token function, when use async in Node 7.2 #1161

Closed
eric7578 opened this issue Dec 21, 2016 · 14 comments
Closed

Unexpected token function, when use async in Node 7.2 #1161

eric7578 opened this issue Dec 21, 2016 · 14 comments

Comments

@eric7578
Copy link

eric7578 commented Dec 21, 2016

In my case, I try to run without babel, so I use node v7.2.0
in my source code:

exports.create = async function (mail, password) {
...
}

But I got the following error

/Volumes/HD/projects/stores/mailIdentity.js:3
exports.create = async function (mail, password) {
                       ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:78:16)
@sindresorhus
Copy link
Member

The common mistake is not including the correct Babel plugin if you don't use the AVA defaults.

@eric7578
Copy link
Author

Thanks for the reply! But, with Node 7, I can run async/await without any babel plygin. I do run the code with Node(without plugin), and it works. The problem is that I can run with the same code with Node 7, but I cannot run it with avajs.

@novemberborn
Copy link
Member

with Node 7, I can run async/await without any babel plygin. I do run the code with Node(without plugin), and it works.

As far as I can tell Node.js 7 currently ships with V8 5.4, and async / await is only in 5.5: http://v8project.blogspot.co.za/2016/10/v8-release-55.html. How are you managing this?

@cliguo
Copy link

cliguo commented Dec 26, 2016

I encountered similar problems,with Node 7, I can run async/await,but ava:
async findById(table, id, columns) {
^^^^^^^^
SyntaxError: Unexpected identifier

@mightyiam
Copy link
Contributor

@cliguo async function findById(table, id, columns) { perhaps
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

@EduardoRFS
Copy link

v8 5.4 have async functions when use --harmony-async-await

@novemberborn
Copy link
Member

v8 5.4 have async functions when use --harmony-async-await

@eric7578 @cliguo are you using this flag?

@EduardoRFS
Copy link

Ava shebang dont pass flags to node, you need to run using a command node --harmony-async-await ./node_modules/.bin/ava

@novemberborn
Copy link
Member

novemberborn commented Jan 6, 2017

Ava shebang dont pass flags to node

To clarify, you mean the ava invocation in the npm test script?

@EduardoRFS
Copy link

@novemberborn yep

@novemberborn
Copy link
Member

Ah cool. Then yea, that makes sense. Your workaround is good, though honestly I think folks should stick to Babel until async / await is available without a flag.

@eric7578
Copy link
Author

Thanks for the reply @EduardoRFS , I will try it later ;)

@eric7578
Copy link
Author

Finally back to work :D, I tried @EduardoRFS and it works!
Thanks for every one who provide opinions!
As a record for those who has the same problem, here is my final script:

node --harmony-async-await ./node_modules/.bin/ava NODE_ENV=test ./**/*_test.js

@cdyfng
Copy link

cdyfng commented Jun 9, 2017

check this link :
trufflesuite/truffle#146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants