I'm running my app using
sudo npm run dev
how should I use pm2 in order to run sudo npm run dev
this command.
Asked
Active
Viewed 4.2k times
26

Asmita Bhiware
- 383
- 1
- 3
- 9
-
would you please show how your `dev` script is defined in the `package.json` file? – Shimon Brandsdorfer Mar 20 '17 at 19:35
-
` "scripts": { "dev": "NODE_ENV=development nodemon index | myscript" }` – Asmita Bhiware Mar 20 '17 at 19:50
-
@ShimonBrandsdorfer any idea? – Asmita Bhiware Mar 20 '17 at 19:50
-
1I see you use `nodemon`, do you need `nodemon` if you use `pm2`? – Shimon Brandsdorfer Mar 20 '17 at 22:38
-
You can just do `"scripts": { "dev": "NODE_ENV=development pm2 index | myscript" }'. Please let me know if it works – Shimon Brandsdorfer Mar 20 '17 at 22:42
5 Answers
66
The following works for me.
pm2 start "npm run dev" --name myAppName
Your app name will be myAppName
You can see the logs
pm2 logs "myAppName"

zenny
- 866
- 7
- 11
-
You can also specify the app name with --name argument, like this: `pm2 start "npm run dev" --name myAppName` Then you can see logs by `pm2 logs myAppName` Also, please don't use `sudo` until it's really necessary (in most cases, pm2 can work fine without sudo). – Maxím G. Jan 26 '19 at 22:20
2
The answer given by @zenny is correct, I want to add that
pm2 start "npm run dev" --name myAppName
try running the above command logged in as a root user instead of a non-root user. I had the same error in AWS ubuntu, it worked me.

Abhishek Kumar
- 33
- 1
- 5
0
In your package.json
file, you will define the scripts for npm to run.
So, you can change your file to look something like this:
"scripts": {
"dev": "pm2 start app.js"
},

Shimon Brandsdorfer
- 1,673
- 9
- 23
0
Also: pm2 start npm -- run dev
(I didn't need to use the app name bits)

Darren Shewry
- 10,179
- 4
- 50
- 46