1

I want to start my script with pm2 in Windows, but I get the error: SyntaxError: Unexpected token ':'

my start script is start:dev and to run that I say: pm2 start npm -- 'start:dev'

my script:

"scripts": {
    "start": "NODE_ENV=production node dist/src/index.js",
    "build": "tsc -p . && ncp productionenv ./dist/src/.env",
    "start:dev": "npm run build:dev",
    "build:dev": "nodemon src/index.ts --exec ts-node src/index.ts -e ts,graphql",
    "test": "jest"
  },

Full err:

App [gateway:0] exited with code [1] via signal [SIGINT]
PM2        | App [gateway:0] starting in -fork mode-
PM2        | App [gateway:0] online
0|gateway  | C:\PROGRAM FILES\NODEJS\NPM.CMD:1
0|gateway  | :: Created by npm, please don't edit manually.
0|gateway  | ^
0|gateway  | SyntaxError: Unexpected token ':'
0|gateway  |     at Object.compileFunction (node:vm:352:18)
0|gateway  |     at wrapSafe (node:internal/modules/cjs/loader:1031:15)
0|gateway  |     at Module._compile (node:internal/modules/cjs/loader:1065:27)
0|gateway  |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
0|gateway  |     at Module.load (node:internal/modules/cjs/loader:981:32)
0|gateway  |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
0|gateway  |     at Object.<anonymous> (C:\Users\xx\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
0|gateway  |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
0|gateway  |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
0|gateway  |     at Module.load (node:internal/modules/cjs/loader:981:32)
PM2        | App [gateway:0] exited with code [1] via signal [SIGINT]
PM2        | Script C:\PROGRAM FILES\NODEJS\NPM.CMD had too many unstable restarts (16). Stopped. "errored"
Mj Ebrahimzadeh
  • 587
  • 9
  • 22
  • 1
    check this answer https://stackoverflow.com/questions/31579509/can-pm2-run-an-npm-start-script – salman Feb 23 '22 at 09:44
  • @salman did exactly what they said `pm2 start npm --name "gateway" -- run "start:dev"` and still getting same error – Mj Ebrahimzadeh Feb 23 '22 at 09:46
  • i have used the same command and its working fine, check you package.json file if you have that script or not? – salman Feb 23 '22 at 10:12

3 Answers3

4

You can use following way to run the script :

pm2 start npm --name "your-app-name" -- run "start:dev"
undefined
  • 66
  • 5
3

You are actually running the custom script, so it needs to be run in a different way:

pm2 start npm run start:dev

Whenever you add any custom script npm run scriptname is the command to follow. If you want to know more about it, check here.

Also, it should be a part of the script:

"scripts": {
    "start": "NODE_ENV=development npm start",
    "start:dev": "npm start --watch",
    "start:debug": "npm start --debug --watch",
    "start:prod": "node dist/main",
  },
Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
1

Only for who run NextJs.

should create deploy.json file:

{
    "apps": [
        {
            "name": "dicom-interactive",
            "script": "./node_modules/next/dist/bin/next",
            "args": "start",
            "env": {
                "PORT": "3000",
            }
        }
    ]
}

run

pm2 start deploy.json
Tan Nguyen
  • 947
  • 7
  • 8