switching node versions using n - node.js

[root#xx.xx.xx.xx xxxx]# n
installed : v14.8.0 to /usr/local/bin/node
active : v10.21.0 at /bin/node
Installed node version 14.8.0 using n. Not sure how 10.21.0 was installed. Cannot delete or switch versions. I am using pm2 process manager and need the versions switched i.e dont want to run n run v14.8.0 server.js i would rather change the active version globally, so that running pm2 start would not need to specify a specific version. Please help.

First, you can explicitly select /usr/local/bin/node by creating a file like
/usr/local/bin/node server.js
Make sure to rename it to something ending with .sh
Then to run it with pm2 you can do pm2 run yourfilename.sh
Second, you can use a shebang line in server.js, so the first line must be
#!/usr/local/bin/node
And you can run your script the way you were doing it before

Related

PM2 Process Manager is not showing the actual version from each project package.json version but NVM version number

I'm running PM2 list on MacOS, and those projects node are running of the nvm managed node version. by right the expectation on the versioning column it should be showing individual node process version based on their package.json version number respectively.
but it's showing the version of nvm (node version manager). which is odd. Anyone has a clue or is this a bug from PM2 ? It happens for the couple of teammate who are working on MacOS either.
I finally understand the logic how pm2 works.
the current setup node was managed by nvm and pm2 is installed globally inside the node version.
when those process above was started by using the following style:
{
...
script: 'npm',
args: 'run start:auth'
...
}
in the pm2 config.js (start script), it indirectly ask PM2 to refer to the nvm folder, to use the npm there. at such when we inspect the process by invoking pm2 info auth it shows this line, this gave us the hint, pm2 would read the package.json resides in the ~/.nvm folder, thus it becomes the version from nvm.
Therefore the mystery is resolved.
NOTE: PM2 would read the first available package.json from the repository root, or somewhere available within the script path (e.g. when we do the pm2 info <process>). with that said, in normal circumstance we only use pm2 in a production environment, and we tend to use it to start the actual *.js script, and it would process the package.json correctly without any issues.

Automatically use the right version of Node for a package

I know it is possible to switch between different versions of Node using NVM, n, or similar.
Is there a convenient way for the right version of Node to automatically be used when running commands within a given package? ("Right version" being determined by the engine tag or similar).
For instance, I would like to be able to do this:
cd mypackage-that-needs-node10
npm run serve
# ... node 10 is used
cd ..
cd mypackage-that-needs-node14
npm run serve
# ... node 14 is used
n supports an engine label to find Node.js version from package.json, and auto which includes more sources such as a .node-version file.
https://github.com/tj/n#specifying-nodejs-versions
For example:
$ n install engine
# or run the target version of node
$ n run auto index.js
# or execute a command with that Node.js in path
$ n exec auto npm run serve
A possible approach is to install node itself into your package, and have npm run scripts use it in preference to the system version of node. See node

$ babel and $ babel-node don't launch the REPL

I'm trying to setup babel on Ubuntu 14.04 but it doesn't seem to be working!
Here are some outputs that may be required:
$ which node
/usr/sbin/node
$ which nodejs
/usr/bin/nodejs
$ which babel
/usr/local/bin/babel
$ which babel-node
/usr/local/bin/babel-node
When I execute babel or babel-node the prompt just returns. The same happens on executing the commands with a filename as argument. (The file has just console.log("hello").
How do I fix this?
The /usr/sbin/node vs /usr/bin/nodejs issue has been covered in Cannot install packages using node package manager in Ubuntu but basically Ubuntu has a separate node package that is NOT Node.js. The package for Node.js on Ubuntu is called nodejs. If you have both installed, it means your scripts will try to run using the other unrelated application. One option is to symlink nodejs to node.
The best solution however would be to use something like nvm to install node for your user without installing it globally. Then you can install and update node versions extremely easily, and your PATH will always reference node properly.
Changing node to nodejs in the first line of /usr/local/bin/babel-node and /usr/local/bin/babel solves it.

Forever: With two node versions installed, specify a node version to run with

I need 2 versions of NodeJS to run two different applications. One version is available in the default path so I can run it by calling node. The other version is installed at ~/node-v10/bin/node.
When I do forever start app.js, it's started with the first version. I
How do I start a forever script with the second node version? Forever doesn't appear to have a configuration to allow me to specify the node path.
I've tried this, but it doesn't work. Forever still starts with the old version:
NODE_PATH=~/node-v10/bin/node forever start app.js
You can also use -c to specify the command:
forever start \
-c /opt/node-0.8.22-1e7b3d5/bin/node \
/var/www/app/server.js
-c defaults to node, so normally forever will just use the first node in the $PATH.

setting up node-supervisor for nodeJS

Hi I'm a nodeJS and command line noob. I've managed to get a linux box up running and everything is fine. However restarting the node scripts every time I make changes is beginning to frustrate.
So I'm trying to set up node supervisor.
https://github.com/isaacs/node-supervisor
All installed fine but
when I try to set it up using the following from the command line (putty)
supervisor test.js
I get
supervisor: command not found
Does anyone have any idea why this would be?
many thanks
it means supervisor isn't in your path or wasn't set with execute permissions. Find supervisor then use the full path to it. it's possibly still in the build folder if you forgot to install it system-wide.
You should install supervisor globally with -g command option
> npm install -g supervisor

Resources