Application of cross-env node parameter setting method of setting parameters and PM2

parameter settings

In node development process, we often use npm run dev / build command

But sometimes we need to set some parameters need to manually install cross-env in the window system

npm i cross-env -D

 

Then add the parameter in the script 

"scripts": {
  "test": "cross-env NODE_ENV=development node index.js"
}

So that we can freely set the parameters we need.

 

Use PM2, for the application to open a long process

After our line in the command line window, use nmp run build our project started, when the window is closed, our application process also will be closed.

At this time, we need to introduce PM2. In our global cloud server installation

npm install pm2 -g

 

Then start the application and then pm2. So even closed my current xshell window. I still can access the application.

pm2 start/stop index.js

 

Or provided in the script

"scripts": {
  "test": "cross-env NODE_ENV=development node index.js"
 "build": "pm2 start/stop index.js"
}

 

PM2 startup parameter settings of application

In the root directory of the new ecosystem.config.js profile

= module.exports { 
  Apps: [ 
    { 
      // production 
      name: "Prod" ,
       // start of the project entry file 
      Script: "./index.js" ,
       // project environment variable 
      env: {
         "NODE_ENV": "Production's" 
      } 
    }
  {
      // test environment 
      name: "the Test" ,
       // start the project entry file 
      Script: "./index.js" ,
       // project environment variable 
 env: {  "NODE_ENV": "the Test"  }  }
] }

 

Of course, here you apart from setting up a production environment configuration, testing and pre-development environment is the same with the form, accept the configuration objects of different environments apps by this array.

Reconfiguration in our script in

"scripts" : {
     "Test": "Cross-Node The index.js the env NODE_ENV = Development" ,
     "dev": "The index.js --harmony Supervisor" ,
     "Build": "Start ecosystem.config.js PM2 - Prod --watch only " // here I only use the online PM2 
  },

 

When we npm run test or npm run build

Let's get our NODE_ENV parameters process.env.NODE_ENV in node environment.

 

Guess you like

Origin www.cnblogs.com/shichangchun/p/11069926.html