Unexpected Token async ()

Viewed 12025

I am having a strange issue, as my code is working fine in Ubuntu and Windows machines and is failing in a Centos server. I have the same node version 8.9.1 and the same npm 5.5.1 and the same sails 1.0.0.41 (globally and locally). Everything works except on my Centos machine where I get

const makeRequest = async () => {
                          ^
SyntaxError: Unexpected token (

with an arrow pointing to the first paren. The only thing that I currently suspect is that my N version management has not properly updated node. Running node -v reports 8.9.1. Here is a simplified cut of the async code:

const makeRequest = async () => {
  try{
    const user = await sails.models.user.findOne({id: user_id});
    return Promise.resolve(user);
    }
  catch(error){
    sails.log.error('error getting data', error);
  }
}

return makeRequest().then(out => {
  return Promise.resolve(out);
});

Any suggestions on how to resolve this error?

3 Answers

There is missing } in the code. Check below

const makeRequest = async() => {
  try {
    const user = await sails.models.user.findOne({
      id: user_id
    });
    return Promise.resolve(user);
  } catch (error) {
    sails.log.error('error getting data', error);
  }
} // -> Its misssing in your code
return makeRequest().then(out => {
  return Promise.resolve(out);
});

Edited

OP's response

The issue was version management.

node -v gave me 8.9.1 sudo node -v gave me 6.11

The solution was to chown the folder for my user (rather then root), and run the application without sudo. NVM then worked correctly. Accepting the other answer as there were errors in my code.

The issue was version management.

node -v gave me 8.9.1 sudo node -v gave me 6.11

The solution was to chown the folder for my user (rather then root), and run the application without sudo. NVM then worked correctly. Accepting the other answer as there were errors in my code.

Had similar problem,

I have also tried to sudo the npm install, causing below error first, and causing the async unexpected token error.

npm WARN checkPermissions Missing write access to /home/mohanbabu/node_modules
npm WARN enoent ENOENT: no such file or directory, open '/home/mohanbabu/package.json'
npm WARN mohanbabu No description
npm WARN mohanbabu No repository field.
npm WARN mohanbabu No README data
npm WARN mohanbabu No license field.
npm ERR! path /home/mohanbabu/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/home/mohanbabu/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/home/mohanbabu/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/home/mohanbabu/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/home/mohanbabu/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/mohanbabu/.npm/_logs/2019-10-13T16_51_12_932Z-debug.log

(async () => { ^ SyntaxError: Unexpected token ( at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.runMain (module.js:611:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:160:9)

  1. removing the package-lock and running the npm install is the fix.( without sudo )
  2. check your node version
  3. run the js script ( for my project npm start )
Related