Forever Commandline with NPM arguments - node.js

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

Related

Node & React - npm start - is running BUT localhost:3000 "cant be reached"

(having node version 12 and npm version 6)
Backend: Node.js
Front: React.js
i cloned repository
cd into the directory
ran
npm install (installing dependencies etc...)
and when i ran
npm start
and i get
[Ben#Mac:~/Desktop/test]$ npm start
> answers-entry-level-exam#1.0.0 start /Users/Ben/Desktop/test
> lerna run start --parallel
lerna notice cli v3.22.1
lerna info Executing command in 1 package: "npm run start"
#ans-exam/server: > #ans-exam/server#1.0.0 start
/Users/Ben/Desktop/test/server
#ans-exam/server: > ts-node-dev index.ts
#ans-exam/server: Using ts-node version 8.5.2, typescript version 3.7.2
#ans-exam/server: server running 3232
which seems like the server is running okay
but localhost:3000 cant be reached
not opening anything
the first time it did open and there was a MacOS popup on the right side of the screen related to node ( i think that is the issue but cant figure out how to fix)
my package.json:
{
"name": "answers-entry-level-exam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"b": "npm run bootstrap",
"bootstrap": "lerna bootstrap",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lerna run start --parallel",
"postinstall": "npm run bootstrap"
},
"author": "",
"license": "ISC",
"devDependencies": {
"lerna": "^3.22.1"
}
}
Thanks!
I cloned again the repo and it fixed that
Cheers to all those who tried to help :)
Try localhost:3232. I see in your output server running 3232

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

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"
}

Nodemon ''npm' is not recognized as an internal or external command

I realize that this is most likely a duplicate question. I'm new to nodemon and I'm trying to establish a server for a Vue JS project with nodemon. I'm trying to run eslint with nodemon and can't figure out why I keep getting the error message. If I remove npm after --exec it will tell me ''run' is not recognized, and if I remove that I will get ''lint' is not recognized and so on.
My package.json file:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run lint && node'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I have also tried this code in my start scripts:
"scripts" : {
"start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
"lint": "./nodemodules/.bin/eslint **/*.js"
}
Where is tells me that "." is not recognized as an internal external command.
I've installed nodemon into my server folder and in the project directory as well as globally. I have done the same with eslint as well.
i had the same problem today. did some google stuff and found that this is not working anymore. so i tried this
"scripts": {
"prestart": "npm run lint ",
"start": "nodemon src/app.js ",
"lint": "./node_modules/.bin/eslint src/*.js"
},
when you npm start node will run the pre-start script before the start script.Once a file being updated this pre-start wont run by the nodemon.So for that we have to call the nodemon events.So create a nodemon.json on root folder and paste following.
{
"events": {
"restart": "npm run lint"
}
}
you can read more nodemon config options from here nodemon config .There are more nodemon events.you can read them from here event restart
PS:im very new to this. :)
EDIT1:
You can use as follows. this dont need a nodemon config;
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
"lint": "eslint --fix **/*.js "
}
for run use npm run dev it will run es lint + nodemon. this is for windows cmd command.if you are using bash terminal, remove \ in "dev" ;
"dev": "nodemon src/app.js --exec "npm run lint --fix && node""
I had the same problem.
For some reason you can't use simple quotes in npm scripts.
Use escaped double quotes instead. This should work:
"start": "nodemon src/app.js --exec \"npm run lint && node\""
install it globally for making it available on path.
npm i -g nodemon
or if using yarn
yarn global add nodemon
and if you tried this approach and it didn't work.
you should try running it locally..
you have to create a script in your package.json like this
"script": {
"server" : "nodemon scriptFile.js" //name of the file you want to run
}
then use,
npm run server
but before it,
install nodemon locally.
check it, if it is available on package.json

NPM: How can I hook SET NODE_ENV in package.json's prestart script?

I have a basic App created using npm init -y. In package.json I have a main entry which points to server.js.
{
"name": "rest-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prestart": "SET NODE_ENV=dev"
}
I am trying to set the NODE_ENV variable in prestart and let npm to call main to invoke npm start. But environment variable set in the prestart is not carry forwarded and is undefined. When I run 'npm start', console outputs that both commands are executed in order.
PS D:\test\RestAPI> npm start
> rest-api#1.0.0 prestart D:\test\RestAPI
> set NODE_ENV=dev
> rest-api#1.0.0 start D:\test\RestAPI
> node server.js
undefined
[undefined] Listening on http://localhost:3000
but when I print the variable from the app, it is undefined. Is there anything that I am doing wrong here, or is this how it is supposed to behave? Is there a way to invoke and set env variable using 'SET NODE_ENV=dev' without chaining it to 'node server.js'
When I combine both in the 'start' as below, then the environment variable is set.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "set NODE_ENV=dev && node server.js"
}
I am testing this on Windows 10, npm version 3.10.10. Appreciate your help.
I know how this can be done in package.json using 'start'. This question is specific to how this can be achieved through 'prestart'.
Thanks.
The short answer is NOT possible.
1. Why :
this is not be possible because each script executed by different processes that npm spawns for this purpose which has its own environment variables.
To realise that, create test project and configure both scripts to be like
"start": "pause&&set VAR1",
"prestart" : "pause&&set VAR1=value&&set VAR1&&pause",
On windows open the task manager and pay close attention how many cmd process es are listed before running the script.
run the command "npm start" and at each request "press any key to continue..." just notice how processes created are created. I attached screenshots for this in order
2. Unless :
you change how npm executes different scripts to use one cmd for all the scripts which I think is complicated and probably will create bugs.
If you want to chain scripts and add env variables along the way then checkout the example given in the cross-env package:
{
"scripts": {
"parentScript": "cross-env GREET=\"Joe\" npm run childScript",
"childScript": "cross-env-shell \"echo Hello $GREET\""
}
}

Execute node http-server locally from cli

If I install http-server locally (without -g flag) how can I run it with local directory as root directory?
In my root project directory I have a node_modules folder, If I want to execute http-server I need to execute $node ./node_modules/http-server/bin/http-server.
I wish to launch http-server with a command like $npm http-server.... If I use $npm start http-server, the server start without ./ as root.
You can call the executable that sits in the module's bin folder like so:
> ./node_modules/http-server/bin/http-server
If you want to do it with an npm command, you can put that in your package.json's scripts collection:
{
"name": "tmp",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"./node_modules/http-server/bin/http-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Then you can just run
> npm start

Resources