The front-end technology: how to run using the ES (import) of the node program

method one:

In the scripts documents package.json domain, configure the following command:

"start": "cross-env NODE_ENV=dev node -r esm server/index.js”

This focus on the command line that node command -r parameter, the parameter value esm.

If you run the program using pm2, you can use something like the following command to start the application:

pm2 start npm -- run <scriptname>

For the above example, it is this:

pm2 start npm -- run start


Second way:

Automatic code conversion when the monitor code changes during development can not be achieved in the above manner, generally we use nodemon to implement code changes function, the following manner may be in development mode, support esm.

"dev": "cross-env NODE_ENV=dev nodemon server/index.js --watch server --exec babel-node"


Three ways:

If used in the project is pure import, without the use of require, you can use the node of --experimental-modules parameters:

"start": "cross-env NODE_ENV=dev node --experimental-modules server/index.mjs”


Guess you like

Origin blog.51cto.com/14565733/2445274