Resolve node SyntaxError: Unexpected token... (solve the problem that Node.js does not support the... Operator in the lower version)
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
- 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
- Install node JS version management tool
npm install -g n
- Install the latest stable version of node
- Use n to switch versions
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
- Start node JS service
forever start xxx.js
The startup succeeded and the solution is completed.
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.