How to execute the start script with Nodemon
Asked Answered
L

28

154

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

Longitude answered 23/11, 2015 at 20:17 Comment(1)
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?Digenesis
R
212

This will be a simple command for this

nodemon --exec npm start
Robrobaina answered 19/9, 2017 at 11:37 Comment(3)
What if I need to run only tests without running the app? You solution runs both.Gwenny
this does run npm start but does not start the node server for me.. how would it know where server.js is ?Frei
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.Darlleen
H
76

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

Hemicycle answered 24/11, 2015 at 0:14 Comment(6)
Sorry, looking for a way to execute the start script with nodemon, not execute nodemon in the start script. Sorry if that wasn't clear.Longitude
Won't npm start fail in production for lack of nodemon?Niki
npm run start not npm startDayna
I'm a beginner with npm. I don't understand why do we need to specify the start script while we can use nodemon out of the box? I just install nodemon with npm and use it directly without specifying any script and it works perfectly.Diaconal
@M.Dhaouadi npm start is an alias for npm run startDemello
nodemon yourfile.js directly from the terminalJeanninejeans
A
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.

Amphibious answered 7/8, 2016 at 20:6 Comment(0)
C
23

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

Colston answered 25/7, 2018 at 2:25 Comment(0)
S
15

Use -exec:

"your-script-name": "nodemon [options] --exec 'npm start -s'"
Sheasheaf answered 23/11, 2015 at 20:23 Comment(5)
Is there any way other than exec to do this?Longitude
Just nodemon [options] --exec 'npm start -s' if you want to do it from the command line.Tessin
@ArnoldRoa Install it.Selfpollination
I have no idea about -s flag within npm start -s.Williawilliam
@Williawilliam It's a shortcut for --loglevel silent (it was just an example).Sheasheaf
L
15

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
Langley answered 21/1, 2020 at 11:2 Comment(0)
I
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
Ibadan answered 12/3, 2018 at 12:7 Comment(2)
But when you run off of app.js you are bypassing everything that ./bin/www does...Led
this open new tab each timeHammerskjold
A
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

Ashwell answered 2/4, 2019 at 13:18 Comment(0)
E
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"
    }

Erotogenic answered 12/1, 2021 at 14:37 Comment(0)
F
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.

Fusiform answered 23/2, 2018 at 0:5 Comment(0)
C
4

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

npx nodemon
Counterespionage answered 2/4, 2021 at 15:14 Comment(0)
P
3

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"
  }
Princessprinceton answered 15/5, 2020 at 14:4 Comment(0)
M
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.

Microcircuit answered 21/6, 2020 at 17:23 Comment(1)
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.Causality
P
3

To run nodemon and ts-node on your project,

npm install ts-node nodemon

Then create a file at root project where package.json is called nodemon.json:

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "ts-node ./src/index.ts"
}

The in package.json add the start script:

{
  "name": "http_file_recieve_server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1"
  }
}

Finally start your project

npm run start

Make sure that at the root of your src folder you have a file called index.ts if thats what you defined your entry point in the nodemon.json file.

Pallas answered 23/11, 2023 at 7:33 Comment(0)
S
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).

Slimsy answered 11/3, 2020 at 16:12 Comment(0)
A
2

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
Arrest answered 3/2, 2023 at 17:36 Comment(0)
F
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" },

Fiesta answered 16/7, 2021 at 14:15 Comment(0)
J
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

Joinery answered 6/4, 2022 at 6:21 Comment(0)
A
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"
  },
Acquittance answered 20/10, 2018 at 23:2 Comment(1)
You can still make it "start-dev": "nodemon ./bin/www" if you install it under devDependenciesIreneirenic
M
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).

Misbecome answered 13/6, 2020 at 13:25 Comment(0)
S
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

Szabo answered 23/2, 2021 at 8:38 Comment(0)
C
0

You can use this instead of npm start :

npx env-cmd nodemon
Carrier answered 8/3, 2022 at 12:32 Comment(0)
M
0

You can do this one:

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

This will run the app.ts for you forever

Mudd answered 14/11, 2022 at 10:4 Comment(0)
S
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

Stephens answered 12/2, 2023 at 17:41 Comment(0)
R
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"
  }
}
Rafi answered 17/7, 2023 at 18:6 Comment(0)
C
0
"start": "node app.ts"
Compliment answered 11/3 at 13:16 Comment(0)
T
0

Update as of 2024

If you have Node.js version 18 or higher, you no longer need Nodemon. You can just use the --watch option:

{
  "scripts": {
    "start": "node --watch my_file.js"
  }
}

Then execute the start script:

npm run start

Further, if you are using Node.js 22 or higher, you can execute the start script with the node command instead of the npm command:

node --run start
Toxin answered 28/4 at 12:12 Comment(0)
A
-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

Antagonist answered 20/12, 2022 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.