Uncaught SyntaxError: Unexpected token '.' ! ! ! Lower versions of node.js do not support the optional chaining operator

 node.js version 13.11.0

1. Problems occur

      When the local node.js project is packaged and run on the server, the project fails to start and an error is reported:


    Uncaught SyntaxError: Unexpected token ‘.‘  

    optional chaining operator from js code

     Check the node version node -v

     The version of the online node is 13.11.0. No error is reported when starting locally. The local node version is 18.12.1.

 

3. Technical introduction

Optional chaining operator: The optional chaining operator was introduced in Swift 5.1, version of the Swift programming language. It is a way to avoid NullPointerException when accessing a property that may be null (null) or calling a method that may be null. It was introduced in some programming languages ​​such as Swift, TypeScript, and JavaScript.

Optional chaining operator, supported in Node.js at least v14.10.0

4. Reasons for error reporting:

In Node.js, supported JavaScript syntax and features depend on the version of the V8 JavaScript engine used. V8 is a high-performance JavaScript engine developed by Google for executing JavaScript code. The optional chain operator is a new feature introduced in the ECMAScript 2020 standard, and the features of the ECMAScript standard require V8 engine support in Node.js.

The V8 engine typically follows updates to the ECMAScript standard relatively slowly to ensure that new features are fully tested and optimized. Therefore, when the optional chaining operator was released as part of the ECMAScript standard, it took some time for the V8 engine to adapt and support this feature.

In versions prior to Node.js v14.10.0, older versions of the V8 engine were used, and these older versions did not support the optional chaining operator. Therefore, in older Node.js versions, you need to use other methods or tools to handle potentially null properties and method calls, such as conditional judgments, short-circuit evaluation, etc.

5. Solution

Upgrade Node.js using your package manager (optional). If you are already using a package manager (such as npm or nvm), you can use them to upgrade Node.js.

Install or switch to a specified version of Node.js: npm install -g node@<version>

For example: npm install -g [email protected]

Upgrading Node.js may require administrator privileges, so you may need to use an administrator account to perform installation and upgrade operations.

Note that this only installs Node.js globally. If you need to use different versions of Node.js in a specific project, consider using the nvm (Node Version Manager) tool to manage multiple Node.js versions

It is also recommended to back up your project code and related configuration files before upgrading to prevent unexpected situations during the upgrade process. If you are using a specific version of Node.js, you may need to modify or test your code to adapt to the new version.

Guess you like

Origin blog.csdn.net/youyudehan/article/details/132196403