Search code examples
macosnpmnode-modules

Cannot update version of node - unexpected token


Downgraded my node recently for a tech assessment to vers 12, now wanted to upgrade but seems something is wrong. Ive tried a heap of things that everyone suggests.

Not a very proficient mac user, so any help appreciated

MacBook-Pro ~ % npm install -g n
ERROR: npm v9.1.2 is known not to run on Node.js v12.22.12. You'll need to
upgrade to a newer Node.js version in order to use this version of npm. This
version of npm supports the following node versions: `^14.17.0 || ^16.13.0 ||
>=18.0.0`. You can find the latest version at https://nodejs.org/.

ERROR:
/usr/local/lib/node_modules/npm/lib/utils/exit-handler.js:22
  const hasLoadedNpm = npm?.config.loaded
                           ^

SyntaxError: Unexpected token '.'
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at module.exports (/usr/local/lib/node_modules/npm/lib/cli.js:76:23)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:2:25)
    at Module._compile (internal/modules/cjs/loader.js:999:30)

-MacBook-Pro ~ % node -v
v12.22.12

Tried npm install -g n


Solution

  • Try using the OSX installer on nodejs.org/. It will update your local node installation and also a compatible npm version (v8.19.2 as of writing). Then the error should be gone when typing node -v.