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
Related
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 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.
I have to set-up node environment in my production system. I installed node in my local system its fine, after that i checked for version node -v.
Now i installed node in my production system using sudo apt get install nodejs and now i checked the version node -v nothing happened but i used nodejs -v, its working.
Also in my local system i used node filename.js to run my node app. But in my production system i have to use nodejs filename.js. I dont know why this happening? Also whether it will create problem in my production. Please share your ideas.
It's more linux specific question. You can create a symlink to use node instead of nodejs. Command example here:
sudo ln -sT $(which nodejs) /usr/local/bin/node
This is because your particular distribution of linux already has a node binary.
You're more than likely not using it for anything. You have two options.
rename the pre-installed node to something else, and rename nodejs to node
rename nodejs to node but make sure to adjust your $PATH so that node.js's directory is loading before the pre-installed node
I have untarred node.js from the tar file given on nodejs.org, but when i try executing my js program through node command nothing happens, but on the other hand nodejs command runs executes the file.
So my question is what's the difference between node command and nodejs command as and will it effect my programs as i didn't build from the source code. And i of that is the reason of this discrepancy.
This is highly dependent on many factors. Mainly, it depends on what node and nodejs in your shell actually are. You can check this using type node / type nodejs and/or which node / which nodejs (or perhaps whereis). This also depends on the OS and the shell.
My guess is that which -a node will yield /usr/sbin/node which is not the nodejs executable and thus why it does not execute your node code. On my system, it is:
/usr/bin/node -> /etc/alternatives/node -> /usr/bin/nodejs
i.e. node is just a symbolic link to nodejs, which is the executable.
You can also create this alias yourself so that it overrides whatever node is for you.
Some of these answers were difficult to understand for me, so I'm going to write the answer that would've helped me.
node is something like a radio telemetry solving program, they just happened to snag the name node first. nodejs is what you're after. So
make sure you:
apt-get install nodejs
then, to fix the lame naming issue, create a symlink. A symbolic link between node and nodejs.
sudo ln -s /usr/bin/nodejs /usr/bin/node
The first part is the original file placement, and then where it should link to.
You could also create an alias in your bash profile, which is also pretty easy.
For sure available 'node' package is not related to nodejs.
Just take a look at this node from here:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed from node to nodejs. You'll need to symlink /usr/bin/node to /usr/bin/nodejs or you could uninstall the Amateur Packet Radio Node Program to avoid that conflict.
So it seems like you may purge that radio program with
dpkg --purge node
And then install nodejs via one of common ways f.e. from precompiled deb packages available like this:
sudo apt-get update
sudo apt-get install -y python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
But nodejs updates faster than packages so after you will get any nodejs version available it's more efficient to use this module for managing nodejs versions - https://github.com/visionmedia/n
At least after some tests this solution looks most suitable for me at mac os.