146

How can I execute the start script from a package.json file with Nodemon?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Citronen
  • 4,050
  • 4
  • 16
  • 20
  • You sure you can execute a start script from package.json? Because from what I know package.json is to list all your dependencies and nodemon is to keep your process running. I am not quite sure what you are hinting at here? – Saras Arya Nov 23 '15 at 20:23

25 Answers25

204

This will be a simple command for this

nodemon --exec npm start
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85
  • What if I need to run only tests without running the app? You solution runs both. – Serg Oct 04 '18 at 10:32
  • 2
    this does run `npm start` but does not start the node server for me.. how would it know where server.js is ? – Sonic Soul Apr 26 '19 at 15:51
  • 1
    This is the correct answer to the question. It is up to the user to make sure their NPM scripts actually do what they want them to. You can use nodemon directly in your scripts, but that's not what was asked. This is the proper way to have nodemon rerun scripts on changes. – Jon Church Jul 07 '21 at 19:08
71

In package json:

{
  "name": "abc",
  "version": "0.0.1",
  "description": "my server",
  "scripts": {
    "start": "nodemon my_file.js"
  },
  "devDependencies": {
    "nodemon": "~1.3.8",
  },
  "dependencies": {

  }
}

Then from the terminal you can use npm start

Nodemon installation: https://www.npmjs.com/package/nodemon

Murat Ozgul
  • 11,193
  • 6
  • 29
  • 32
32

I have a TypeScript file called "server.ts", The following npm scripts configures Nodemon and npm to start my app and monitor for any changes on TypeScript files:

"start": "nodemon -e ts  --exec \"npm run myapp\"",
"myapp": "tsc -p . && node server.js",

I already have Nodemon on dependencies. When I run npm start, it will ask Nodemon to monitor its files using the -e switch and then it calls the myapp npm script which is a simple combination of transpiling the typescript files and then starting the resulting server.js. When I change the TypeScript file, because of -e switch the same cycle happens and new .js files will be generated and executed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MehranTM
  • 714
  • 9
  • 10
21

I use Nodemon version 1.88.3 in my Node.js project. To install Nodemon, see in https://www.npmjs.com/package/nodemon.

Check your package.json, see if "scripts" has changed like this:

  "scripts": {
    "dev": "nodemon server.js"
  },

server.js is my file name, you can use another name for this file like app.js.

After that, run this on your terminal: npm run dev

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sukma Saputra
  • 1,539
  • 17
  • 32
15

Use -exec:

"your-script-name": "nodemon [options] --exec 'npm start -s'"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
14

In package json:

"scripts": {
  "start": "node index",
  "dev": "nodemon index"
},

"devDependencies": {
  "nodemon": "^2.0.2"
}

And in the terminal for developing:

npm run dev

And for starting the server regularly:

npm start
Simeon Leyzerzon
  • 18,658
  • 9
  • 54
  • 82
amixOV
  • 149
  • 1
  • 4
7

First change your package.json file,

"scripts":
    { 
        "start": "node ./bin/www",
        "start-dev": "nodemon ./app.js"
    },

After that, execute command

npm run start-dev
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rkeshri
  • 103
  • 1
  • 8
7

In package.json file. change file like this

"scripts":{ 
   "start": "node ./bin/www", 
   "start-dev": "nodemon ./app.js"
 },

and then execute npm run start-dev

Sohail Ahmad
  • 7,309
  • 5
  • 27
  • 46
5

Add this to script object from your project's package.json file


    "start":"nodemon index.js"

It should be like this


    "scripts": {
        "start":"nodemon index.js"
    }

Aravindh AKC
  • 59
  • 1
  • 1
4

Nodemon emits events upon every change in state; start, restart crash, etc. You can add a Nodemon configuration file (nodemon.json) like so:

{
   "events": {
       "start": "npm run *your_file*"
   }
}

Read more in Nodemon events — run tasks at server start, restart, crash, exit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
King
  • 2,128
  • 1
  • 15
  • 15
4

I simply use 'npx' in the terminal to set up nodemon and execute it

npx nodemon
erbaz kamran
  • 73
  • 1
  • 8
3

If globally installed then

"scripts": {
    "start": "nodemon FileName.js(server.js)",
},

Make sure you have installed nodemon globally:

npm install -g nodemon

Finally, if you are a Windows user, make sure that the security restriction of the Windows PowerShell is enabled.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Satyam
  • 567
  • 1
  • 6
  • 20
  • not true, for this to work, only add nodemon with dev dependencies will do, just that "nodemon" won't be available at your command line if you run it directly. – jimzcc Aug 19 '22 at 04:55
2

You can also install nodemon globally for frequent use:

npm i nodemon -g or sudo npm i nodemon -g

then edit your package.json:

  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },

Generally, 'dev' specifies developmental use (npm run dev).

dhahn
  • 729
  • 9
  • 11
2

It will depend on types of your Nodemon installation. If you install Nodemon globally by using commands (npm install nodemon --global or npm install nodemon -g), you do not have to specify any script for Nodemon in your package.json file. Just executing command nodemon index.js will run your project.

But if you install Nodemon locally by command npm install nodemon then you have to specify the script. If you name it as start then npm run start or npm start will trigger the server to run.

// Absolutely no need for global installation
 "scripts": {
    "start": "nodemon index.js"
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafiq
  • 8,987
  • 4
  • 35
  • 35
1

{ "name": "backend", "version": "0.0.0", "private": true, "scripts": { "start": "nodemon ./bin/www" }, "dependencies": { "bcrypt": "^5.0.1", "cookie-parser": "~1.4.4", "debug": "~2.6.9", "express": "~4.16.1", "hbs": "^4.1.2", "http-errors": "~1.6.3", "morgan": "~1.9.1", "nodemon": "^2.0.12" } }

use "nodemon ./bin/www" scripts > start

  • eg:

"scripts": { "start": "nodemon ./bin/www" },

Shajin KP
  • 27
  • 9
1

Try this, with watch:

nodemon --exec ts-node pathtoapp/filewithserver.ts -e ts

my project example: nodemon --exec ts-node src/server.ts -e ts

1

First install package for nodemon as dev dependencies using

$ npm i nodemon -D

Then your package.json will have:

  "devDependencies": {
    "nodemon": "^2.0.20"
  }

Then you can edit change package.json scripts part as

  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  }

Then you can use command:

$ npm run dev
0

To avoid a global install, add Nodemon as a dependency, then...

package.json

"scripts": {
    "start": "node ./bin/www",
    "start-dev": "./node_modules/nodemon/bin/nodemon.js ./bin/www"
  },
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zipzit
  • 3,778
  • 4
  • 35
  • 63
0

If you have nodemon installed globally, simply running nodemon in your project will automatically run the start script from package.json.

For example:

"scripts": {
  "start": "node src/server.js"
},

From the nodemon documentation:

nodemon will also search for the scripts.start property in package.json (as of nodemon 1.1.x).

Alf Eaton
  • 5,226
  • 4
  • 45
  • 50
0

I know it's 5 years late, if you want to use nodemon.json you may try this,

{
  "verbose": true,
  "ignore": ["*.test.js", "fixtures/*"],
  "execMap": {
    "js": "electron ." // 'js' is for the extension, and 'electron .' is command that I want to execute
  }
}

The execMap will execute like a script in package.json, then you can run nodemon js

Roby Cigar
  • 766
  • 2
  • 14
  • 28
0

You can use this instead of npm start :

npx env-cmd nodemon
Syscall
  • 19,327
  • 10
  • 37
  • 52
ahmet
  • 1
0

You can do this one:

nodemon --exec ts-node src/app.ts

This will run the app.ts for you forever

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
George ak
  • 11
  • 3
0

You can also install nodemon as a development dependency:

npm install --save-dev nodemon or using yarn: yarn add nodemon -D.

With a local installation, nodemon will not be available in your system path or you can't use it directly from the command line. Instead, the local installation of nodemon can be run by calling it from within an npm script (such as npm start) or using npx nodemon.

In other words just run it with npx nodemon index.js

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

This is the right way

{
  "name": "cyber",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --exec ts-node src/app.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@tensorflow/tfjs": "^4.8.0"
  }
}
-1

Just use the command : npx nodemon app.js

This is very simple ! and we can run the below command without altering the package.json file in your project