NVM

NodeJS

Common Quality Gates

  • TypeScript Strict Mode (strict: true), ESLint + Prettier, prohibit meaningless naming.
ScriptPurposeWhy It’s Important
npm run devDevelopment environment startupLocal development, hot update
npm run lintCode style checkPrevent formatting issues
npm run typecheckTypeScript type checkingPrevent type errors
npm run testRun testsEnsure functionality correctness
npm run buildBuild production versionGenerate optimized code

PM2

Windows Installation

Confirm that Node.js and npm are installed

node -v
npm -v

Global Install PM2

npm install pm2 -g

Install Windows Auto-start Support

npm install pm2-windows-startup -g

Install PM2 Service

pm2-startup install

💡 This will create a Windows service, ensuring that PM2 runs automatically after system restart

Verify Installation

pm2 -v

If the version number is displayed, the installation is successful!

PM2 Basic Usage Examples

Start Node.js Application

pm2 start server.js --name "my-api" --watch
  • --name: Assign a name to the process
  • --watch: Automatically monitor file changes and restart

Check Process Status

pm2 list

View Logs

pm2 logs my-api

Stop Application

pm2 stop my-api

Restart Application

pm2 restart my-api

PM2 Common Management Commands

PM2 is a very powerful process manager. The following are the most commonly used commands in your daily maintenance:

Basic Management

  • Start service: pm2 start server.js --name "upload" (start and name as upload)
  • Stop service: pm2 stop upload (pause service, process remains in the list)
  • Restart service: pm2 restart upload (must execute this command after code changes)
  • Delete service: pm2 delete upload (completely remove from PM2 list)

View Status

  • View list: pm2 list (view all process status, memory usage, restart count)
  • View logs: pm2 logs upload (view real-time logs, a debugging tool. Add --lines 100 to see more)
  • Monitor panel: pm2 monit (open a graphical terminal interface, monitor CPU/memory/logs in real time)

Auto-start on Boot (Advanced)

If you want the service to run automatically after computer restart:

  1. Generate startup script: pm2 startup (it will give you a command line, copy and execute it)
  2. Save current list: pm2 save (freeze all running processes in the current list as content for next boot start)

Advanced Configuration

Create configuration file ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'my-api',
    script: 'server.js',
    instances: 'max', // Automatically start according to CPU core count
    exec_mode: 'cluster',
    watch: true,
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }]
}

Start the application:

pm2 start ecosystem.config.js --env production

Why Recommend PM2?

PM2 is a process manager for Node.js applications, helping you achieve application startup, shutdown, restart, monitoring functions, and supports process guardianship (automatically restart the application after it crashes).

  • Does not block command line
  • Automatically restarts applications
  • Log management
  • Auto-start on boot