How can I execute the start script from a package.json file with Nodemon?
This will be a simple command for this
nodemon --exec npm start
npm start
but does not start the node server for me.. how would it know where server.js is ? –
Frei 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
npm start
fail in production for lack of nodemon? –
Niki npm run start
not npm start
–
Dayna npm start
is an alias for npm run start
–
Demello nodemon yourfile.js
directly from the terminal –
Jeanninejeans 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.
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
Use -exec
:
"your-script-name": "nodemon [options] --exec 'npm start -s'"
nodemon [options] --exec 'npm start -s'
if you want to do it from the command line. –
Tessin -s
flag within npm start -s
. –
Williawilliam --loglevel silent
(it was just an example). –
Sheasheaf 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
First change your package.json file,
"scripts":
{
"start": "node ./bin/www",
"start-dev": "nodemon ./app.js"
},
After that, execute command
npm run start-dev
app.js
you are bypassing everything that ./bin/www
does... –
Led 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
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" }
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.
I simply use 'npx' in the terminal to set up nodemon and execute it
npx nodemon
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"
}
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.
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.
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).
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
{ "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" },
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
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"
},
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).
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
You can use this instead of npm start :
npx env-cmd nodemon
You can do this one:
nodemon --exec ts-node src/app.ts
This will run the app.ts
for you forever
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
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"
}
}
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
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
© 2022 - 2024 — McMap. All rights reserved.