Skip to content

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

Open
@eusousu

Description

@eusousu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions