How to add paramater with npm scripts in package.json (NodeJS) - node.js

I want to create a file in given folder from terminal using npm script. I know it works like adding custom script in scripts object of package.json file like:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js",
"addstyle": "touch ./css/stylesheet.css"
},
and then run npm run addstyle from terminal.
But what if I want to pass custom name for my stylesheet from terminal?
I got few options like adding $variable in script:
"scripts": {
...
"addstyle": "touch ./css/$filename.css"
},
and then add filename before the command i.e. filename=homepage npm run addstyle
Is there anyway, I can append a parameter in the end with - or -- like:
npm run addstyle --filename=homepage
. OR
npm run addstyle -filename homepage

Run this command and assign the $FILE_NAME
$FILE_NAME = 'homepage' npm run addstyle
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js",
"addstyle": "touch ./css/$FILE_NAME.css"
}

Related

npm start is starting the server.js not my React app, how can I use it to start my app?

So I was having this issue of node not restarting the server when there changes made on the server.js file. I fixed it by editing the package.json file. However, once fixed, the npm start command starts the server.js file: [nodemon] starting `node server.js
How do I start my react app then? npm start was the command I used before
Most probably your set your package.json file to
"scripts": {
"start": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Change your server package.json to something like this:
"scripts": {
"server": "nodemon server.js",
"test": "echo \"Error: no test specified\" && exit 1"
then run the command
npm run server --this will start your server.js
Nodemon has nothing to do with the client-side(React app). So, before running npm-start:
Make sure you are in the correct directory cd [directory-name]
It's better to separate your front-end and back-end in a separate folder

I am facing problem in running the backend of a github repository

I hope you are doing great,
I am trying to cloning one of the Github repository,
here's the link to repo:
https://github.com/thevarunjain/job-portal-mern-stack
I've done everything according to my knowledge, I've run the MongoDB, install all the dependencies by npm I, frontend is running successfully but whenever I run the backend "npm run dev" am getting this error:
** ./node_modules/.bin/nodemon src/index.js
'.' is not recognized as an internal or external command,
operable program or batch file. **
here's the script of package.json:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "./node_modules/.bin/nodemon src/index.js"
}
Please let me know how can I fix this issue, thanks!

Nodemon does not restart when index.js file is changed

This is the portion from package.json :
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon --watch app index.js"
},
All my code except the index.js, package.json and node_modules resides in a sub folder called app.
When I run using npm run dev , nodemon watches the changes in app folder and restarts if there's any changes. But won't restart if I make any changes in index.js (entry point)
My folder structure:
|-- app/
|-- node_modules/
|index.js <--- nodemon not watching this file
|package.json
|package-lock.json
Why is it so?
EDIT:
Here's the solution (from #Pedro Filipe):
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
If you're importing files into index.js, I think you just need to do nodemon index.js in order to have the files that interest you watched. I suspect that when you pass the flag --watch [folder_name] it basically just ignores the filename you pass afterwards.
Nodemon detects changes into the files and restarts it automatically.
Currently, you are saying node to run the index.js file which requires restart whenever you want to reflect your changes.
All you need to do is change the "start" command.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
}
(Previously commented this answer)
Nodemon watches the entry-point to your project.
Which, In almost all the cases of node project is a single file which eventually imports other files and so on and so forth.
(Assuming that your entry-point is index.js i.e. you're importing your other files in there.) You can simply use the nodemon index.js as the script for your dev
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
Nodemon detects changes into the files and restarts it automatically.
Currently, you are saying node to run the index.js file which requires restart whenever you want to reflect your changes.
All you need to do is change the "start" command.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},

How to stop npm start , when lint or Unit testcase fails?

I am using npm start with lint and unit test case, if lint or test case fails, npm should not up the api, but I am not able to do.
Here is code I am using:
"scripts": {
"start": "gulp lint & npm test & node src/server/index.js",
"test": "mocha --timeout 10000"
},
Please help me what I have to do for it, I searched on internet also, but not getting the proper solution.
You can simply rewrite the code like this:
"scripts": {
"start": "gulp lint && npm test && node src/server/index.js",
"test": "mocha --timeout 10000"
},
Double && will never start execution of node server if previous steps contains errors.
Use && instead of &:
"scripts": {
"start": "gulp lint && npm test && node src/server/index.js",
"test": "mocha --timeout 10000"
},

Forever Commandline with NPM arguments

I'm building a project which I have multiple instances of running, with the exact same code, using forever.
With forever, it's possible to set an id, using --id "id".
{
"name": "project",
"version": "1.0.0",
"description": "Test",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"forever": "forever start --id $1 -c \"npm start\" ./"
},
"author": "Renzo <renzo#geex.company>",
"license": "ISC"
}
This way, I'm trying to make a standard for different app instances like:
npm run forever cms
The idea is that this would start the app with id cms.
But instead, it lists the app with id true.
Can anyone help me?
Try changing this:
npm run forever cms
to this:
npm run forever -- cms
but you'd also have to change the order of arguments in package.json.
Also, you can put this in a script, like forever.sh:
#!/bin/sh
forever start --id $1 -c "npm start" ./
and this in package.json:
"scripts": {
"forever": "sh forever.sh"
},
and then use this from the command line:
npm run forever -- cms
To make it work on Windows see these answers:
Why does `DEBUG=foo node index.js` fails in `scripts` section of `package.json`
'bash' is not recognized as an internal or external command
NPM script under cygwin/windows: the syntax of the command is incorrect
This is more of a workaround than an answer, but for now I'm doing it like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"forever": "forever start --id $ncmsfid -c \"npm start\" ./",
},
And starting it with ncmsfid=cms npm run forever

Resources