Unitech / Unitech/pm2

PM2 can't run npm properly on windows (Is windows not a supported enviroment?)

Open
#5,991 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

The default windows node installation have a CMD script for running npm, usually located at C:\Program Files\nodejs\npm.cmd or wherever nodejs was installed on your device.

PM2 doesn't seem to know how to interpret CMD scripts and attempts to run it with node, that fails on the first line of execution with the following error:

Node.js v22.15.1
C:\Program Files\nodejs\npm.cmd:1
:: Created by npm, please don't edit manually.
^

SyntaxError: Unexpected token ':'
    at wrapSafe (node:internal/modules/cjs/loader:1662:18)
    at Module._compile (node:internal/modules/cjs/loader:1704:20)
    at Object..js (node:internal/modules/cjs/loader:1895:10)
    at Module.load (node:internal/modules/cjs/loader:1465:32)
    at Function._load (node:internal/modules/cjs/loader:1282:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49

The current workaround I found (here) is to create a pm2.json file with the following properties:

{
    "apps": [
        {
            "name": "<your-app-name>",
            "script": "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
            "args" : "<start|run [script]>",
            "exec_mode": "cluster"
        }
    ]
}

and exec it like:

pm2 start pm2.json

differently from the resource:

  • the exec_mode: cluster needs to be present or else you will have a unclosable shell host spwn with the process. (this seems as another error, plz let me know if it already exists here, if not I can create the issue)
  • the interpreter: none property need to be removed or you'll get a [PM2][ERROR] Process failed to launch spawn EFTYPE.
  • the instances property didn't seem to make any change so I removed it.
  • the "cwd" seems to be optional.

The problem seems to be well known as of #5571, #5871, #5608, #5616, #5551, #5428 going back at least 2 years but with no commentary from maintainers, which troubles me most! Is windows not a supported enviroment? It would be of much help if this was addressed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Windows launch using the npm.cmd path described in the issue, then compare it with the npm-cli.js workaround in pm2.json and review related issues #5571, #5871, #5608, #5616, #5551, and #5428. Done should mean npm scripts launch through PM2 on Windows without the workaround or an unclosable shell host.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.