Node error: "SyntaxError: Unexpected token {" at "const {join, basename} = require('path')"

Viewed 1330

I need to work with an app based on ionic framework v1 and Cordova 7.1, but there is the following problem with Node when running Cordova:

$ cordova -v
/node_modules/cordova/node_modules/npm-normalize-package-bin/index.js:3
const {join, basename} = require('path')
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (node_modules/cordova/node_modules/read-package-json/read-json.js:14:27)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

I installed Cordova with this command:

npm install -g cordova@7.1.0

The project requires the following versions which are installed on my machine:

node -v
v5.12.0

npm -v
3.8.6

ionic -v
1.7.15

grunt --version
grunt-cli v0.1.13
grunt v0.4.5

bower -v
1.8.4

plugman -v
2.0.0

Does anyone have a clue where the problem is? I'm glad for any help.

3 Answers

Cordova depends on "init-package-json" which in turn depends on "read-package-json: 1 || 2".

Now there's been an update to "read-package-json" some months ago which introduces a new dependency to "npm-normalize-package-bin". This package makes use of a syntax construct that's apparently supported not before node 6.0.0.

So, if updating node is not an option (which is recommended, since node 5 - 8 is end-of-life), you may install this (also rather outdated) cordova locally (inside the project folder) and overwrite the read-package-json dependency there by npm i read-package-json@2.0.13.

Edit: Call cordova via npx: npx cordova -v

Thank you all for your helpful inputs to better understand the cause.

Unfortunately, updating node was not an option. In the end, I compared my Cordova installation with a teammate who does not have this problem. The only difference was that his 'cordova/node_modules/npm-normalize-package-bin' was not present where the error is triggered. So I deleted this package from my system to solve the problem.

Maybe this will help somebody else.

Related