pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

PM2 is unable to run server.js on Windows 10

Open nitinmukesh opened this issue 9 years ago • 58 comments

I'm opening this issue because:

  • [x] pm2 isn't behaving like it should do.

What's going wrong?

I am unable to start node application using PM2 I have node version 7.5.0 npm version 4.4.4 PM2 version 2.4.3 Windows 10

I am trying to start the app using

pm2 start npm -- start

Package.json

{
  "name": "eems-api",
  "version": "1.0.0",
  "description": "Enterprise Expense Management API",
  "main": "server.js",
  "scripts": {
    "start": "npm run apidoc && babel-node server.js",
    "clusters": "babel-node clusters.js",
    "apidoc": "apidoc -i routes/ -o public/apidoc",
    "test": "SET NODE_ENV=test mocha test/**/*.js"
  },
  "apidoc": {
    "name": "EEMS API - Documentation",
    "template": {
      "forceLanguage": "en"
    }
  },
  "author": "Nitin Mukesh",
  "license": "restricted",
  "dependencies": {
    "babel-cli": "^6.5.1",
    "babel-preset-es2015": "^6.5.0",
    "bcrypt": "^0.8.7",
    "body-parser": "^1.15.0",
    "compression": "^1.6.1",
    "consign": "^0.1.2",
    "cors": "^2.7.1",
    "express": "^4.15.2",
    "helmet": "^1.1.0",
    "jwt-simple": "^0.4.1",
    "memwatch-next": "^0.3.0",
    "morgan": "^1.6.1",
    "passport": "^0.3.2",
    "passport-jwt": "^2.0.0",
    "sequelize": "^3.19.2",
    "sqlite3": "^3.1.8",
    "winston": "^2.1.1"
  },
  "env": {
    "PYTHON": "D:/Python27"
  },
  "devDependencies": {
    "apidoc": "^0.15.1",
    "babel-register": "^6.5.2",
    "chai": "^3.5.0",
    "mocha": "^2.4.5",
    "supertest": "^1.2.0"
  }
}

error log

D:\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at Object.exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Object.<anonymous> (C:\Users\nitin.mukesh\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

it works fine using npm start

server.js

import express from "express";
import consign from "consign";

const app = express();

consign({verbose:false})
	.include('libs/config.js')
		.then('db.js')
			.then('auth.js')
				.then('libs/middlewares.js')
					.then('routes')
						.then('libs/boot.js')
							.into(app);

module.exports = app;

nitinmukesh avatar Apr 03 '17 09:04 nitinmukesh

D:\XAMPP\htdocs\nodewebservice\eems>pm2 start npm -- start [PM2] Applying action restartProcessId on app [npm](ids: 0) [PM2] npm ✓ [PM2] Process successfully started ┌──────────┬────┬──────┬───────┬─────────┬─────────┬────────┬─────┬──────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │ ├──────────┼────┼──────┼───────┼─────────┼─────────┼────────┼─────┼──────────┼──────────┤ │ npm │ 0 │ fork │ 10432 │ online │ 45 │ 0s │ 23% │ 8.1 MB │ disabled │ │ server │ 1 │ fork │ 0 │ errored │ 15 │ 0 │ 0% │ 0 B │ disabled │ └──────────┴────┴──────┴───────┴─────────┴─────────┴────────┴─────┴──────────┴──────────┘ Use pm2 show <id|name> to get more details about an app

nitinmukesh avatar Apr 03 '17 09:04 nitinmukesh

Could you please provide your windows/pm2/nodejs version ?

vmarchaud avatar Apr 03 '17 09:04 vmarchaud

I provided that in my first post

I have node version 7.5.0 npm version 4.4.4 PM2 version 2.4.3 Windows 10

nitinmukesh avatar Apr 03 '17 10:04 nitinmukesh

@nitinmukesh My bad sorry Do you by any chance use the Bash on Windows feature to use PM2 ? Could you tell me which shell you are using ?

vmarchaud avatar Apr 03 '17 11:04 vmarchaud

Sorry I did not used the template to add post, my bad.

I am using Command prompt. I search for cmd and right click > run as administrator. Not using anything else. Sorry if I missed any steps but not sure.

but my server works fine with the following command npm start OR npm run clusters

is there any conflict with the package(s) included in package.json

nitinmukesh avatar Apr 03 '17 11:04 nitinmukesh

Do you use babel at runtime ? If yes, please read http://pm2.keymetrics.io/docs/tutorials/using-transpilers-with-pm2 Otherwise i don't see other possibility so i'll need a simple file to reproduce the issue myself and if needed fix the problem inside pm2.

vmarchaud avatar Apr 03 '17 15:04 vmarchaud

Thank you vmarchaud. I am using ES6 and using babel. If I understand I need to create production build and then use pm2.

nitinmukesh avatar Apr 03 '17 17:04 nitinmukesh

If you want to keep the cluster mode you need to use require('babel-register'); in another file (read the doc), if you don't care just run :

  • npm install -g babel-cli
  • pm2 start --interpreter babel-node server.js

vmarchaud avatar Apr 03 '17 18:04 vmarchaud

I have same issue on Win10.

"scripts": {
    "start": "better-npm-run start-dev",
},
"betterScripts": {
    "start-dev": {
      "command": "webpack-dashboard -- node ./implements/server.dev.njs",
      "env": {
        "NODE_ENV": "development",
        "PORT": 3000
      }
    },
module.exports = {
  apps: [
    // Static application server
    {
      name: 'server',
      script: 'npm',
      args: 'start',
    }
  ],
};

And when i try pm2 start ecosystem.config.js

0|server   |     at Object.Module._extensions..js (module.js:579:10)
0|server   | C:\PROGRAM FILES\NODEJS\NPM.CMD:1
0|server   | (function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
0|server   |                                                               ^
0|server   | SyntaxError: Unexpected token :
0|server   |     at createScript (vm.js:56:10)
0|server   |     at Object.runInThisContext (vm.js:97:10)
0|server   |     at Module._compile (module.js:542:28)
0|server   |     at Object.Module._extensions..js (module.js:579:10)
0|server   |     at Module.load (module.js:487:32)
0|server   |     at tryModuleLoad (module.js:446:12)
0|server   |     at Function.Module._load (module.js:438:3)
0|server   |     at Object.<anonymous> (C:\Users\Vera\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
0|server   |     at Module._compile (module.js:570:32)
0|server   |     at Object.Module._extensions..js (module.js:579:10)

How i can start npm-scripts? it is possible?

konstantin24121 avatar Apr 18 '17 21:04 konstantin24121

same issue here also

C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^
SyntaxError: Unexpected token :
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Object.<anonymous> (C:\Users\admin\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:53:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
       

krisdigitx avatar May 17 '17 10:05 krisdigitx

This is closed, but no explanation how to solve this problem. Anyone?

romanran avatar Jun 07 '17 10:06 romanran

pm2 for windows sucks!

krisdigitx avatar Jun 27 '17 12:06 krisdigitx

+1

sifex avatar Oct 12 '17 10:10 sifex

+1, this is still broken, it seems to stem from the fact that it's reading node.cmd in C:\Program Files\nodejs and trying to throw that in some function. Does anyone have any idea what it's supposed to be reading on Windows instead?

rjerue avatar Dec 04 '17 16:12 rjerue

I am too having the same issue. Any solutions Guys !!

softmantk avatar Jan 04 '18 12:01 softmantk

Same issue here :confused:

MrToph avatar Jan 18 '18 18:01 MrToph

Still running into this issue. Anyone was able to solve?

jasrub avatar Feb 14 '18 21:02 jasrub

Have the same issue

szymon-wiatr avatar Feb 20 '18 09:02 szymon-wiatr

Same issue here on Windows 10. I'm using LTS(8.9.4) version of node.

tresdev avatar Feb 20 '18 16:02 tresdev

I was struggling with this for a few days. The issue is that on windows, PM2 will try to interpret npm.cmd (presumably in JS), rather than literally execute it. If I had to guess, this isn't a problem on unix because pm2 knows to use bash (it doesn't have a cmd default interpreter), so it only looks like it's executing a command line function, or something along those lines - I haven't actually looked at the specifics of how NPM installs on unix. That's also why specifying an interpreter can sometimes fix the issue as @vmarchaud suggested.

Luckily the workaround is easy, and probably a better practice anyway. Our solution was to point the "script" option at a new js script that uses node-cmd to run "npm start". EG: var cmd=require('node-cmd'); cmd.run('npm start');

theaceofthespade avatar Feb 20 '18 18:02 theaceofthespade