I have a node.js application that I start using forever like so:
NODE_ENV=production forever start index.js
I've also worked out how to setup crontab to automatically start the forever command for this application on server reboot:
#reboot /usr/local/bin/forever start /path/to/my/app/index.js
The only problem here is the node environment. How do I add the production environment to the crontab command?
If you want to set just one environment variable, you could use the export command right before the forever command.
#reboot export NODE_ENV=production; /usr/local/bin/forever start /path/to/my/app/index.js
For more than one variable Sukima's method is better.
If you need to execute a special command with variables etc. in a crontab it's easier to write a simple shell script and call that script from the crontab:
#!/bin/bash
export NODE_ENV=production
/usr/local/bin/forever start /path/to/my/app/index.js
Make it executable: chmod 755 /usr/local/bin/start_my_app.sh
Then in your crontab:
#reboot /usr/local/bin/start_my_app.sh
Related
In my ec2 instance I am able to run pm2 command.
But while deploying application through code deployment I get this error.
LifecycleEvent - ApplicationStop
Script - application_stop.sh
[stdout]Stopping any existing node servers
[stderr]/opt/codedeploy-agent/deployment-root/878477e5-6ffb-4175-8e9e-97045ea99290/d-HVRQ58IBL/deployment-archive/application_stop.sh: line 4: pm2: command not found
My application_stop.sh code.
#!/bin/bash
#Stopping existing node servers
echo "Stopping any existing node servers"
pm2 stop main
As per #ranjanistic I checked my pm2 path using which pm2 command and it returned
~/.nvm/versions/node/v16.15.1/bin/pm2
After that I update my application_stop.sh using this below command
~/.nvm/versions/node/v16.15.1/bin/pm2 start main
Also added symbolic link like this to npm, node and pm2.
///this process worked. Thanks #ranjanistic
which npm
which node
which pm2
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/npm
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/node
sudo ln -s /home/ec2-user/.nvm/versions/node/v16.15.1/bin/pm2
Still not working
The binary executable reference to your command needs to be available in the environment you're expecting to run it from.
You are using npm to run a pm2 command, which means it is installed as a local module. Therefore you can similarly create another npm script like npm run stop:all with your pm2 command, it should work.
If you're running it in a bash script, the command reference binary should be available in PATH. Or you can also use your command by mentioning its binary path instead of name, independent of wherever the binary is located, for example
If pm2 is installed as a global node module
/usr/bin/pm2 stop main # or whatever the path to the binary is.
Or if pm2 is installed as a node module in project then
./node_modules/bin/pm2 stop main # again, path to pm2 binary can be anything, you'll have to know beforehand
Also, I'd recommend a separate config file for each of your pm2 applications, so that you can use it anywhere without worrying whether your main app is available to pm2 or not.
You may also need to check if npm or node commands are running or not, and based on that you may add the path to your folder containing pm2 in $PATH variable before running the deployment. You can check the path to pm2 manually using which pm2 if it is available.
You need to provide absolute path like this /usr/bin/pm2
I'm just starting to learn about how JavaScript, HTML, and Electron all work, and I want to know what runs electron . in the "scripts" -> "start" of package.json, because I can't tell what does and that kind of wizardry makes me nervous.
According to the man pages for npm, what npm start does is that it reads the package.json, looks at the script under "scripts" -> "start" -> some_script, and then runs some_script. Sometimes, some_script is something like node foobar.js, which makes sense to me, since I can run that from the command line. NodeJS is executing foobar.js. However, in the case of the electron-api-demos, some_script is electron .
You can download and run electron-api-demos via
git clone https://github.com/electron/electron-api-demos
cd electron-api-demos/
npm install && npm start
In order to try to figure out what is running electron ., I've run it in the node shell, and I've tried running node main.js. I've even tried opening up the node shell and running
electron-api-demos#2.0.2 start $DIR/electron-api-demos
electron .
(which is exactly the output of npm start). None of them worked, because none of them started up the Electron application. At this point I'm very puzzled at how, exactly, the start script gets executed at all.
So I guess my question is: does there exist a command (that I can use on the command line) to start up this Electron application, without using npm? If not, what is npm calling to start up this Electron app?
I apologize if this question has been asked before, but I all the sources I found didn't seem to go into any further detail about what, exactly, is done when npm start is run and how it executes electron . . Thank you for your time!
Command line interfaces installed with npm are put in the node_modules/.bin/ directory. You can't just run them from the command line because that directory isn't in your PATH (unless you put it there, or you installed it globally).
So, if you want to run electron without npm start, you can run ./node_modules/.bin/electron .. Since this is a bit verbose, newer versions of npm provide the command npx to run things without the ./node_modules/.bin/ part, so npx electron . also works.
Since npm scripts often use the packages you've installed, they automatically add node_modules/.bin/ to the PATH before running your command. As a result, the start script can just reference electron directly.
npx can do some other cool things too – npm has a blog post about it.
When you run npm start , it by default run command corresponding "start" key of script property of package.json like
"script":{
"start": "ng serve",
"launch":"electron main.js" or "electron ." // main.js located in the same dir
"test": " ng test"
}
same when you run npm run launch it will trigger the command corresponding of the "launch" key of script property of package.json file. like run electron main.js command and your application will launched.
so if you want to run the your electron application directly like electron main.js then install the electron module globally using command npm install electron -g then simply run the electron main.js command and your application will start.
I am new to nodeJS and Jake but in my company they are using it to run unit tests.This is how they are running unit tests through ant
<exec executable="cmd" dir="${nodeJsTests.basedir}/../nodejs/">
<arg value="/C"/>
<arg value="start cmd /C "npm install & .\node_modules\.bin\jake local dir=${basedir} --trace & pause"" />
</exec>
From what I understood is they are doing the following things in this piece of code, do correct me if I am wrong
Going to nodejs driectory.
Installing jake at a particular location (.\node_modules.bin\jake)
Run unit tests
I want to achieve the same(run the tests), without using ant.I think I am able to do first two steps but stuck in the third step.I tried running command - Jake local from various directories but no success
If anyone can help me on this?
It's easier to see what's happening if you parse the string in the second arg value:
npm install & .\node_modules\.bin\jake local dir=${basedir} --trace & pause
This is a shorthand way of essentially running these 3 commands in order:
npm install
.\node_modules\.bin\jake local dir=${basedir} --trace
pause
The first command installs all the dependencies defined in your package.json file (which presumably includes jake).
The second command runs the local version of jake (the one that is installed inside the node_modules folder after running the previous step).
The third command is simply a cmd util to pause execution.
As long as you have installed the dependencies you should have no problem running jake without ANT (simply run the 2nd command above, replacing the ${basedir} value, and make sure you run it in the same directory that your package.json is located in).
I have a Node.js file in c:\scripts and want to execute them in Windows command from anywhere by node my-file.js. It's kind of requirement for execute a bat file, which I can put the folder in %PATH%. I've tried to include c:\scripts in %NODE_PATH% but it doesn't work.
nodejs has the notion of executables ... for example package
jslint
once installed can be directly executed from anywhere ... its installed using the -g flag as in :
npm install -g jslint
just like normal packages its code is installed in dir defined by env var
NODE_PATH
additionally its executable lives in the bin dir which is by default added to PATH during initial nodejs install
on linux
type jshint
jshint is /home/stens/node-v5.8.0/bin/jshint
echo $NODE_PATH
/home/stens/node-v5.8.0/lib/node_modules
on windows box similar happens just examine your PATH to see the nodejs bin dir ... on linux its a location relative to $NODE_PATH
UPDATE :
Alternative to above you can put a some-file (do Windows equivalent to chmod +x some-file to make it an executable) into a dir listed in your $PATH ... for example on linux if I issue
type jslint
which is found because PATH contains dir /home/stens/node-v5.8.0/bin ... above command outputs :
jslint is hashed (/home/stens/node-v5.8.0/bin/jslint)
then show contents
cat /home/stens/node-v5.8.0/bin/jslint
#!/usr/bin/env node
var main = require("../lib/main.js");
main.runMain(main.parseArgs());
you can see the executable file in just nodejs code with the 1st line
#!/usr/bin/env node
which tell the system to execute it using node (dunno the Windows way but similar must be available)
so its executed using
jslint blah input-blah parm-blah goes-blah here-blah
Hopefully this tips you in the right direction
I'm using ES6 with babel-node to create my app and I require my app to start with the command babel-node app.js. This command is listed in scripts: start in my package.json so the command npm start runs the correct command.
Open shift starts node apps with node + what ever script is set in the main property of your package.json file. In my case its "main": "app.js". So this command is run node app.js.
The server is choking on the first ES6 it encounters which makes sense. I can't figure out how to configure openshift to run babel-node or npm start to start up my app.
Here is my package.json file -> https://gist.github.com/jkinman/2cc57ce5fae5817d6bca
You shouldn't run your server with babel-node, which is memory intensive and not meant for production. Instead, you should use the require hook by creating a file start.js (name unimportant) with the following content:
require('babel-core/register')
require('./app.js')
// or server.js or whatever you use to normally start you app
Then you can start your server with node start.js.