I have installed pm2 module using the following command:
npm install pm2#latest
Then I tried to start my test application using pm2 as follows:
$ pm2 start test.js
It throws the following error:
'pm2' is not recognized as an internal or external command
Do i need to set environment variable for pm2?
You need to install PM2 globally via npm install --global pm2#latest and if you want to use the local version, try ./node_modules/.bin/pm2 start test.js.
After installing PM2, we may need to add following value to path variable under Environment Variables
C:\Users\USERNAME\AppData\Roaming\npm
After adding, reopen the command prompt.
You might installed the pm2 locally instead of global scope, this is due to missing -g parameter in your installation command.
npm install -g pm2
or
yan add -g pm2
If you tried npm install pm2 then the module will install locally to the app that you are currently developing from there you can invoke the pm2 using,
./node_modules/pm2/bin/pm2 start index.js
But it won't mostly work on windows. Try to use the global install option.
You are getting the same error after global install option then add the npm path in your environment variables.
Related
I install volta as node version handler and when I run pm2 status/start/stop in PowerShell or cmd, there is
'pm2' is not recognized as an internal command' error.
I know that I should install pm2 as global but run npm i -g pm2 it is not working and have same error. Anybody has any idea?
by this answer of Volta and global npm package I just run volta install pm2 instead of npm i -g pm2 and set Volta/bin in environment variable of windows. in Global installs done right said:
By running volta install mocha, you let Volta know that you want mocha to be available everywhere in your terminal
When I run nodemon bin/www inside my app, it works perfectly, when I try to run nodemon app, it throws this error:
'\"node .\bin\www\"' is not recognized as an internal or external command,
operable program or batch file.
I believe this happened after I received a notification from nodemon today to run npm install -g nodemon to update the nodemon library, after the installation, I got this error:
Please try running this command again as root/Administrator.
I tried to run the cmd as administrator, again, the same problem. Any solution?
step 1: run npm install -g nodemon
step 2: verify installation npm list -g nodemon
Now you can run the app by two ways
nodemon ./server.js localhost 8080
Any output from this script is prefixed with [nodemon], otherwise all output from your application, errors included, will be echoed out as expected.
2nd just type nodemon
If you have a package.json file for your app, you can omit the main script entirely and nodemon will read the package.json for the main property and use that value as the app.
With a local installation, nodemon will not be available in your system path. 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.
or
It turns out that you don’t have to run the command again as
Administrator, and doing so won’t fix the problem. Try
npm cache clean first.
check this for more detail
https://www.npmjs.com/package/fixmynode
command against as root administrator
how-to-fix-node-js-npm-permission-problems
I think you are getting this error because you did not install it as a global module. Try to install nodemon using the -g flag.
$ npm install -g nodemon
Install the nodemon module globally using
npm install -g nodemon
Also install the module in the package in your project where the package json is present using
npm install nodemon -save
Check if the nodemon is present in the json list.
Now run the js file using any of the following
nodemon
nodemon app.js
nodemon app
Use the following command line in an windows environment (1st cd to your apps root directory):
nodemon ./bin/www
I am working with npm on a web app and I found an issue when using some packages that requires terminal commands to run such like nodemon and concurrently
I installed it via
sudo npm install --save-dev nodemon
and when I try to use it via:
nodemon ./server.js
I get an error
nodemon command not found
and the same when I used concurrently
I tried also with
sudo npm install --save nodemon
and it doesn't work.
it only work if I installed it globally
sudo npm install -g nodemon
Why I can't use it when install locally?
Note: I can found the executable file at node_modules/.bin
but this following not working as well
node_modules/.bin/nodemon ./server.js
Global packages can be launched directly because they are saved in your PATH directory by default. If you saved a package locally you can see it on node_modules/.bin/ as you mentioned. So there are 2 ways to achieve what you want if you want to run an executable package if installed locally:
You can run it via terminal as ./node_modules/.bin/nodemon yourscript.js
Or via npm scripts in your package.json file, you do this:
{
"scripts": {
"nodemon": "nodemon yourscript.js"
}
}
and execute npm run nodemon.
The 2nd approach works for both packages installed globally or locally.
I prefer installing packages locally, so my other apps won't get affected especially if I'm using different package versions per project.
UPDATE
On npm#5.2.0 onwards, it comes with a binary called npx. So you can run specific packages on the terminal just by npx [package] and it executes either your local or global npm package. In your case it should be something like npx nodemon server.js.
Because it's in your node_modules/.bin folder, not your PATH.
You can either use ./node_modules/.bin/nodemon or $(npm bin)/nodemon to call nodemon.
To run any locally installed npm module (Mocha, Eslint, Nodemon, etc.), you can now use npx. Try npx nodemon server.js.
I also recommend setting main within your package.json to point to the script you want to run (index.js by default), so you could just run npx nodemon or nodemon (if globally installed) and it will know which script to run.
This is because the local node_modules folder is not in your PATH. See the link to the duplicate question for more details.
After updating Node.js from v10.16 to v10.32, PM2 was not detected, however it was running fine when checked with ps aux. Even upon system reboot PM2 functioned correctly even though manual PM2 commands resulted in following type of error.
pm2 list
pm2: command not found
Switching Node.js back to 10.16 and PM2 commands were again available. fyi PM2 was initially installed under v10.16.
While in v10.32 tried PM2 install command npm install pm2 -g but had to use command npm install pm2 -g --unsafe-perm to get operational.
Node.js v10.16 now runs PM2 v10.1.
Node.js v10.32 now runs PM2 v10.8.
Is this the proper method to keep PM2 versions in sync and working with Node upgrades/changes? Does this need to occur after installing every new version of Node?
It's seem there no way without re-installing PM2 after a Node update :-(
$ nvm install 6.11.3 --reinstall-packages-from=6.11.2 && nvm alias default 6.11.3
$ nvm uninstall 6.11.2
$ pm2 update # Update in memory pm2
$ pm2 startup
$ nano /etc/init.d/pm2-init.sh # Wrong path :-(
But re-installing pm2 is not enought, some things are still broken even if it seem to work, logs are no more in real time for example
My hot fix :
$ rm -rf /root/.pm2
$ pm2 reload pm2.json --env production
$ pm2 startup ubuntu
Do not forget to rebuild packages after updating the version of node.js:
cd /to/root/of/your/project
npm rebuild
npm i -g pm2 && pm2 update
# here 0 and dist/main.js change for your project
pm2 delete 0 && pm2 start dist/main.js
In console :
pm2 save --First make sure that you saved correctly all your processes
npm install pm2 -g --Then install the latest PM2 version from NPM
pm2 update --And finally update the in-memory PM2 process
When you switch node versions, you also switch the packages, so you need to reinstall pm2 on node update. Fortunately this does not happen very often.
You could make a shell sript to do both in one go.
For the unsafe-perm thing, it comes only if you install pm2 as root. It makes sense when you think that pm2 has quite a lot of control over your machine's processes.
I tried a lot of times with differnent combinations but still seems not very stable and smart solution. Hence I am listing some of the logic I can think of something which may be you can apply and monitor the result as you upgrading and writing the scripts.
Basically in my situation we have a bunch of applications running under Node. So things getting complex when you need PM2 to launch anothe application that also installed under Node Version Managers, like NVM
Ex. I have
nvm ls
-> v14.17.6
PM2 is installed under:
which pm2
~/.nvm/versions/node/v14.17.6/bin/pm2
Since I am using App1 (a NodeJS app managed by npm). I got:
which App1
~/.nvm/versions/node/v14.17.6/bin/App1
So every time I upgrade using nvm:
nvm install --lts --reinstall-packages-from=14 --latest-npm
Then nvm using a newer version in this console. e.g. 14.7.999999
Perhapse I (at most of the time) needs to get my PM2 plus other applications upgrade at the same maintenance window, I use ncu, ncu -g and upgrade them.
Now, the applications end up with all new version. Ex. a new PM2 instance (local) and an old PM2 running (In Memory) with old comsumer applications (App1) in an old node folder. New version of App1 now exists in new Node application folder but not running.
In memory PM2 version: 5.1.0
Local PM2 version: 5.1.1
Anyway, if you don't have an upgraded version of PM2, you probably still looking for a new path of PM2 installed under your new Node folder. If not, you can install PM2 again with the upgraded node
npm i -g pm2
The thing getting worse is PM2 is in system startup scripts which needs to be re-written. Ex.
/etc/systemd/system/pm2-xx.service
So I endup with vanishing all applications:
pm2 stop app1 && pm2 delete app1
pm2 stop app2 (verdaccio json startup config) && pm2 delete app2
...
pm2 stop appN && pm2 delete appN
Then do:
pm2 update
To swap to the new PM2 instance
Then re-provision all the applications
pm2 start app1, app2, ... appN
Then do
pm2 update
To update the list of applications, check if correct Node path is used.
If all applciation paths are corrected
Do
pm2 startup systemd
And copy and run the suggested Startup Script
sudo env PATH=$PATH:/....
Finally run
pm2 save
To freeze the list on startup.
I want to use nodemon for monitoring my node.js app's, then I execute the next line command:
npm install -g nodemon
or
npm install nodemon -g
When I move to my app folder and try to to
nodemon app.js
The system tells to the next:
"nodemon 'is not recognized as an internal or external command, program or batch file.
Since node prefix is not in the PATH ENV variable , any of the globally installed modules are not getting recognized.
Please try this.
Open cmd prompt
npm config get prefix
append the resulting path to PATH env variable.
Now you should be able to run nodemon from any location.
This is what i have done on my local machine
C:\>npm config get prefix
C:\Users\username\AppData\Roaming\npm
C:\>set PATH=%PATH%;C:\Users\username\AppData\Roaming\npm;
C:\>nodemon
31 Jul 22:30:29 - [nodemon] v0.7.8
31 Jul 22:30:29 - [nodemon] to restart at any time, enter `rs`
31 Jul 22:30:29 - [nodemon] watching: C:\
31 Jul 22:30:29 - [nodemon] starting `node `
^CTerminate batch job (Y/N)? Y
I also got same error as you with this command:
$ sudo npm install -g nodemon
I just really switched as "root" and then just ran:
$ npm install -g nodemon
I think npm has a bug to not work with sudo, but it works fine when you are really "root".
Single line solution
In terminal
npm install -g --force nodemon
There is a problem with integrated terminal of vs code. when I try in external terminal nodemon works. But in integrated terminal, it gives bash: nodemon: command not found error.
so here is my solution
install nodemon as development dependency
npm install --save-dev nodemon
and change package.json of the project
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"nodemon": "./node_modules/.bin/nodemon"
},
to run nodemon type into terminal in project folder
npm run nodemon
Mine was I went to Control Panel and Repair the NodeJS app and tried to install again with npm install -g nodemon and now it works. Maybe you mixed up or something with Node.
check out here :-
npm install -g nodemon
and then run
$nodemon server.js
You won't need to install nodemon anymore, since Nodejs has finally introduced its --watch feature which restarts the process when an imported file is changed.
node --watch index.js
https://nodejs.org/en/blog/release/v18.11.0/
Linux users: I would highly suggest not using sudo or root user to install npm packages. This could become a security problem especially on a production system. I would also suggest not trying to hack permissions as I have hosed an Ubuntu system by not reading the warning on the npmjs procedure.
It would be better to configure npm to use a folder owned by the current user. Simplest approach
wget https://raw.githubusercontent.com/pcnate/npm-configure/master/add-npm-global.sh -q -O - | bash
npm install -g nodemon
Or get the code script on github to see how it works
See details on the npmjs website
On Windows, I was having issues installing nodemon directly from the Command line. Downloaded Cygwin and I was able to npm install nodemon instantly.
You can add path to node packages in System Path variable.
Add "C:\Users\UserName\AppData\Roaming\npm".
Even after adding path to System Path variable it did not work for me using nodemon. Then i used npm run serve to run the server. now it is up and running. Btw i am a windows user
This command worked for me.
If your global installation didn't work then install it in your
development dependency.
npm install --save-dev nodemon
Updated
After Path settings we also need to type in the following commands
Set-ExecutionPolicy Unrestricted
what this command enables running scripts on the system
I think some of us can't reach global environments without admin privileges.
If you tried everything and it's still not working, try running VSCode as administrator. It worked out for me.
had the same problem otherwise was just working fine a day ago.
Very simple fix
first check if nodemon exists on your system globally or not
To check
npm list -g --depth=0
If you don't see then install
it npm install -g nodemon (g stands for globally)
If you see it still doesn't work then you need to configure environment variable
I use Windows OS. On Windows navigate to
Control panel>System>Advanced System Settings>Environment Variables>double-click on PATH
Now check if you have this PATH C:\Users\yourUsername\AppData\Roaming\npm
If not, you will see some existing paths, just append to it separating with semicolon. That's it! Worked for me.
For me node was installed in C:..\Roaming\npm and for you if the PATH is different, you will put in whatever applcable.