Problem with Starting strapi application using pm2 in Windows 10 & Windows Server 2019

System Information
  • Strapi Version: 3.5.4
  • Operating System: Windows 10 / Windows Server 2019
  • Database: MongoDB
  • Node Version: 14.16.1
  • NPM Version: 6.14.4

Describe the bug

Unable to start the strapi application properly on Windows using pm2. Starting with ecosystem.config.js gives the error SyntaxError: Unexpected token ‘:’ at C:\PROGRAM FILES\NODEJS\NPM.CMD. Trying the other option using node-cmd opens blank cmd.exe window with npm title and able to start the application on browser, but closing the blank cmd window stops the application.

Steps to reproduce the behavior

In ecosystem.config

module.exports = {
  apps: [
    {
      name: 'str-api-app',
      script: 'npm',
      args: 'start-production',
      env: {
        NODE_ENV: "development",
      },
      env_production: {
        NODE_ENV: "production",
      },
      exp_backoff_restart_delay: 100,
    },
  ],
};

In package.json

{
  "name": "str-api-app",
  "private": true,
  "version": "0.1.0",
  "description": "A Strapi application",
  "scripts": {
    "develop": "strapi develop",
    "start": "strapi start",
    "build": "strapi build",
    "strapi": "strapi",
    "production": "cross-env NODE_ENV=production npm run build",
    "start-production": "cross-env NODE_ENV=production npm start"
  },
  "devDependencies": {},
  "dependencies": {
    "cross-env": "^7.0.3",
    "node-cmd": "^4.0.0",
    "strapi": "3.5.4",
    "strapi-admin": "3.5.4",
    "strapi-connector-mongoose": "3.5.4",
    "strapi-plugin-content-manager": "3.5.4",
    "strapi-plugin-content-type-builder": "3.5.4",
    "strapi-plugin-email": "3.5.4",
    "strapi-plugin-upload": "3.5.4",
    "strapi-plugin-users-permissions": "3.5.4",
    "strapi-utils": "3.5.4"
  },
  "author": {
    "name": "A Strapi developer"
  },
  "strapi": {
    "uuid": "<some alphanumeric id>"
  },
  "engines": {
    "node": ">=10.16.0 <=14.x.x",
    "npm": "^6.0.0"
  },
  "license": "MIT"
}

I could start the ecosystem.config.js using pm2 start ecosystem.config.js but the admin site wasn’t loading on a browser. Checking the pm2 logs was showing the following error.

1|str-api-app | C:\PROGRAM FILES\NODEJS\NPM.CMD:1
1|str-api-app | :: Created by npm, please don't edit manually.
1|str-api-app | ^
1|str-api-app | 
1|str-api-app | SyntaxError: Unexpected token ':'
1|str-api-app |     at wrapSafe (internal/modules/cjs/loader.js:979:16)
1|str-api-app |     at Module._compile (internal/modules/cjs/loader.js:1027:27)
1|str-api-app |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
1|str-api-app |     at Module.load (internal/modules/cjs/loader.js:928:32)
1|str-api-app |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)
1|str-api-app |     at Object.<anonymous> (C:\Users\CM-10\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
1|str-api-app |     at Module._compile (internal/modules/cjs/loader.js:1063:30)
1|str-api-app |     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
1|str-api-app |     at Module.load (internal/modules/cjs/loader.js:928:32)
1|str-api-app |     at Function.Module._load (internal/modules/cjs/loader.js:769:14)

After following the forums and comments on Github I got to know about the node-cmd module. So, I created a startscript.js which have the following code.

var cmd = require('node-cmd');
cmd.run('npm run start-production');

And then run the command pm2 start startscript.js. It was able to start the application without the previous pm2 error : Unexpected Token ‘:’. But after running pm2 start startscript.js, it immediately opens up a blank cmd.exe window with ‘npm’ as the window’s title. The strapi application opens on a browser until the blank cmd.exe window is open, but once the blank cmd window is closed the strapi application stops loading.

I am not sure if I’m missing anything in the configuration or the way I’m trying to start the application is wrong. Please help.
Thank you.

Hi @sranjan-m,

Thanks for sharing this, helped a lot.

Did you ever got to fix the blank screen popup after running pm2 start startscript.js ?

Thanks.