others-How to solve SyntaxError: Unexpected token error when using `npm install xxx`

1. Purpose

In this post, I would demo how to solve the SyntaxError: Unexpected token when using npm install xxx, just as follows:

[[email protected] ~]# npm install -g --unsafe-perm node-red
/usr/bin/node-red -> /usr/lib/node_modules/node-red/red.js
/usr/bin/node-red-pi -> /usr/lib/node_modules/node-red/bin/node-red-pi

> [email protected] install /usr/lib/node_modules/node-red/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build

node-pre-gyp ERR! stack /usr/lib/node_modules/node-red/node_modules/make-dir/index.js:31
node-pre-gyp ERR! stack 		...defaults,
node-pre-gyp ERR! stack 		^^^
node-pre-gyp ERR! stack
node-pre-gyp ERR! stack SyntaxError: Unexpected token ...
node-pre-gyp ERR! stack     at createScript (vm.js:56:10)
node-pre-gyp ERR! stack     at Object.runInThisContext (vm.js:97:10)
node-pre-gyp ERR! stack     at Module._compile (module.js:549:28)
node-pre-gyp ERR! stack     at Object.Module._extensions..js (module.js:586:10)
node-pre-gyp ERR! stack     at Module.load (module.js:494:32)
node-pre-gyp ERR! stack     at tryModuleLoad (module.js:453:12)
node-pre-gyp ERR! stack     at Function.Module._load (module.js:445:3)
node-pre-gyp ERR! stack     at Module.require (module.js:504:17)
node-pre-gyp ERR! stack     at require (internal/module.js:20:19)
node-pre-gyp ERR! stack     at Object.<anonymous> (/usr/lib/node_modules/node-red/node_modules/@mapbox/node-pre-gyp/lib/install.js:13:17)
node-pre-gyp ERR! System Linux 3.10.0-957.el7.x86_64
node-pre-gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/node-red/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/lib/node_modules/node-red/node_modules/bcrypt
node-pre-gyp ERR! node -v v6.17.1
node-pre-gyp ERR! node-pre-gyp -v v1.0.7

The core error is:

node-pre-gyp ERR! stack /usr/lib/node_modules/node-red/node_modules/make-dir/index.js:31
node-pre-gyp ERR! stack 		...defaults,
node-pre-gyp ERR! stack 		^^^
node-pre-gyp ERR! stack
node-pre-gyp ERR! stack SyntaxError: Unexpected token ...

2. The environment

[[email protected] ~]# npm version
{ npm: '3.10.10',
  ares: '1.10.1-DEV',
  http_parser: '2.8.0',
  icu: '50.1.2',
  modules: '48',
  napi: '3',
  node: '6.17.1',
  openssl: '1.0.2k-fips',
  uv: '1.42.0',
  v8: '',
  zlib: '1.2.7' }

3. The solution

3.1 Why did this error happen?

The node.js and npm version is too low.

Node.js’s latest version is 17, but we are using node.js 6.

We should upgrade our node.js and npm version.

3.2 The solution to resolve this issue

We should upgrade node/npm , we can use the n tool to achieve this goal:

[[email protected] ~]# npm install -g n
/usr/bin/n -> /usr/lib/node_modules/n/bin/n
└── [email protected]

[[email protected] ~]# n latest
  installing : node-v17.1.0
       mkdir : /usr/local/n/versions/node/17.1.0
       fetch : https://nodejs.org/dist/v17.1.0/node-v17.1.0-linux-x64.tar.xz
   installed : v17.1.0 (with npm 8.1.2)

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/bin/node
         new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"
[[email protected] ~]# PATH="$PATH"
[[email protected] ~]#

What is n?

N is just for Node.js version management: no subshells, no profile setup, no convoluted API, just simple.

You can get more by reading this post.

After upgrading, let’s check the local npm/node version:

[[email protected] ~]# npm version
  npm: '8.1.2',
  node: '17.1.0',
  v8: '',
  uv: '1.42.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '102',
  nghttp2: '1.45.1',
  napi: '8',
  llhttp: '6.0.4',
  openssl: '3.0.0+quic',
  cldr: '39.0',
  icu: '69.1',
  tz: '2021a',
  unicode: '13.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'

Now try the npm install command again:

[[email protected] ~]# npm install -g --unsafe-perm node-red

added 266 packages, and audited 267 packages in 3m

26 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New patch version of npm available! 8.1.2 -> 8.1.4
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.1.4
npm notice Run npm install -g [email protected] to update!
npm notice
您在 /var/spool/mail/root 中有邮件
[[email protected] ~]# node-red
24 Nov 17:03:55 - [info]

Welcome to Node-RED

24 Nov 17:03:55 - [info] Node-RED version: v2.1.3
24 Nov 17:03:55 - [info] Node.js  version: v17.1.0
24 Nov 17:03:55 - [info] Linux 3.10.0-957.el7.x86_64 x64 LE
24 Nov 17:03:57 - [info] Loading palette nodes
24 Nov 17:04:00 - [info] Settings file  : /root/.node-red/settings.js
24 Nov 17:04:00 - [info] Context store  : 'default' [module=memory]
24 Nov 17:04:00 - [info] User directory : /root/.node-red
24 Nov 17:04:00 - [warn] Projects disabled : editorTheme.projects.enabled=false
24 Nov 17:04:00 - [info] Flows file     : /root/.node-red/flows.json
24 Nov 17:04:00 - [info] Creating new flow file
24 Nov 17:04:00 - [warn]

Your flow credentials file is encrypted using a system-generated key.

If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.

You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.

24 Nov 17:04:00 - [info] Server now running at
24 Nov 17:04:00 - [info] Starting flows
24 Nov 17:04:00 - [info] Started flows
^C24 Nov 17:05:06 - [info] Stopping flows
24 Nov 17:05:06 - [info] Stopped flows

It works!

4. Summary

In this post, I demonstrated how to solve the ‘SyntaxError: Unexpected token’ error when using ‘npm install’ to install packages, the core reason of this issue is that we have a very old/deprecated version of npm/node, after upgrading the tools by using n, we solved the issue. That’s it, thanks for your reading.