Search code examples
node.jsnpmnodemon

Whats the proper way to start nodemon on a deploy environment?


I seem to be having an issue running nodemon from node_modules in a deployed instance.

I have roughly this in my package.json

{
   ...
  "version": "0.0.3",
  "main": "dist/src/server.js",
  "description": "Persistence Layer",
  "engines": {
    "node": "~6.7"
  },
  "scripts": {
    "start": "nodemon",
  },
"dependencies": {
    ...
    "nodemon": "^1.11.0",
    ...
  }
}

I have the following in my nodemon.json file

{
  "restartable": "rs",
  "verbose": true,
  "debug": 5858,
  "delay": 1,
  "watch": [
    "dist/",
    "node_modules/"
  ],
  "ext": "js",
  "args": [
    "--debug=5858",
    "--max_old_space_size=6384",
    "--optimize_for_size",
    "--max_executable_size=6384",
    "--stack_size=6384"
  ]
}

When i try npm run start i get the following:

jrlil@28178a64e860:/app# npm run start
npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info lifecycle [email protected]~prestart: [email protected]
npm info lifecycle [email protected]~start: [email protected]

> [email protected] start /app
> nodemon

sh: 1: nodemon: Permission denied

npm info lifecycle [email protected]~start: Failed to exec start script
npm ERR! Linux 3.10.0-514.16.1.el7.x86_64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start"
npm ERR! node v6.9.1
npm ERR! npm  v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: `nodemon`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the [email protected] start script 'nodemon'.
npm ERR! Make sure you have the latest version of node.js and npm installed.

However, when i run it using the following everything works as expected.

$node node_modules/nodemon/bin/nodemon.js
[nodemon] 1.12.1...

Why isn't npm run able to look into my node_modules folder and start nodemon?


Solution

  • This is actually more of a Linux question than a Node question, as it's a permissions issue -- the script nodemon being ran by npm does not have the right permissions.

    If you use npm run start to invoke nodemon and you have the right permissions (e.g. root), npm will "hand off" the execution to nodemon and in the process possibly change the user to one without root permissions to be safe:

    From the docs:

    If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.

    If you run node_modules/nodemon/bin/nodemon.js yourself (and you have root permissions), it bypasses that "hand off" so that nodemon.js is run with root permissions.

    The most correct way to deploy a node application is to use something like pm2 to manage processes, and not use nodemon since nodemon is mostly used to watch for changes and restart the server (which is mostly only useful in development contexts). If you still want to use nodemon, you can combine it with the forever package with nodemon like explained here.