Resolve node SyntaxError: Unexpected token

Resolve node SyntaxError: Unexpected token... (solve the problem that Node.js does not support the... Operator in the lower version)

problem

Our tmall elf service uses node JS. Today, I added a package nzh to the project. I was in package JSON, and then run npm install. After running, the nzh package will be installed, and other packages that need to be updated will also be updated.
Then Debug, the test passes without problem, and then publish it to the server.
Run forever start XX on the server JS, stopped.
After reading the log, the error is reported. There is no problem with the test environment, but the error is reported in the production environment.
Errors are reported as follows:

/xxx/node_modules/mqtt/node_modules/ws/lib/websocket.js:345
      ...options
      ^^^
SyntaxError: Unexpected token ...
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/xxx/node_modules/mqtt/node_modules/ws/index.js:3:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

SyntaxError: Unexpected token ...
Translate Chinese into
Syntax error: unexpected token
Obviously, the error is, Syntax error. This must be the problem that the ES feature does not support. That is node JS version

solve

  • View server node JS version is 6.9.2
  • View the test environment node JS version is 12.13.1

Sure enough, the versions are inconsistent, considering the node of the server JS version is really a little low, so I decided to upgrade to the latest stable version

  1. Install node JS version management tool
npm install -g n
  1. Install the latest stable version of node
npm lts
  1. Use n to switch versions
n


4. View node JS version

It's strange that the switch failed
Search found the reason:

N the default installation path is / usr/local. If your node is not in this path, n switching versions cannot copy bin, lib, include and share into this path, so we must pass n_ Modify the default node installation path of n with the prefix variable.

For details, please refer to
node. The principle and solution of JS version management tool n invalidity

  1. Start node JS service
forever start xxx.js

The startup succeeded and the solution is completed.

reflection

Although the problem is solved.
Maybe you'll have more questions ❓❓❓

  • What is the... Operator?
  • ... operator really does not support node V6 9.2?

Considering that the... Operator has a lot to introduce, I won't write it in this article.
See the next article, "JavaScript... Spread operator."

Tags: Javascript node.js ECMAScript ElasticSearch

Posted by xatter on Wed, 18 May 2022 10:56:37 +0300