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
Related
My Problem
I have installed fnm (Fast Node Manager) from this github repo and it works all great except for installing global npm packages. For example, the well-known package nodemon is something I want installed globally and not im my node_modules project directory.
When installing the package globally there seems to be no problem:
And when checking the global package list, nodemon seems to be there:
But when running the command nodemon I get the following output:
As also seen in the fnm repository documentation there is a need to run this piece of code eval "$(fnm env --use-on-cd)"; on load in order to get fnm to work properly and this is what I have done in the .bashrc file.
Note
I am using windows 10, seems to be working on my mac laptop.
The Question
How can I have a global npm package installed for all or at least a single fnm node version? And what I mean by this, is that by running fnm use <NODE_VERION> you specify what node version to use as also seen in the repository documentation. I want to be able to run the nodemon command without it being installed in a project's node_modules directory.
You do not need to delete the multishells. The problem is the Git Bash path.
Fix is here: https://github.com/Schniz/fnm/issues/390
Put this in your .bashrc
eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH
if [[ -f .node-version || -f .nvmrc ]]; then
fnm use
fi
As mentioned this actually worked on my OS X machine (aka my mac book pro) but not on my windows 10 computer. The solution I came up with after analyzing thoroughly the behaviour of fnm is the following:
Go to C:\Users\<YOUR_USER>\AppData\Local\fnm_multishells and delete the directory if it exists.
When downloading global packages do it via CMD or any terminal which isn't bash (or the terminal that has the "$(fnm env --use-on-cd)"; script) as this makes fnm then search for the global package in the wrong place.
This approach mitigates any path errors as I found that this was the core problem. As shown in the screenshot above when trying to run nodemon it looks for it in C:\Program Files\Git\Users\Valeri..... but this directory simply does not exist. After removing the directory mentioned in step 1 fnm stops looking for nodemon in that path and instead uses the one installed via CMD.
Essentially, the "$(fnm env --use-on-cd)"; script allows us to use fnm properly but at the same time causes this issue. Simply download global npm packages from a terminal that does not run this command.
Edit
I just had the same issue and to confirm you don't even need to delete the fnm_multishells directory. Just run npm -g remove <whatever> and install it via cmd or powershell. A command-line which does not run "$(fnm env --use-on-cd)"; on load.
[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
I would like to know whether it is possible somehow to run a node.js command line app without using the global folder, i.e. no npm install -g or npm link.
If you want to use a cli that is npm installed locally without using anything else, you can do (assuming webpack):
node ./node_modules/.bin/webpack
Just check that directory ./node_modules/.bin
Probably npx would work for you.
$ npm i -D webpack
$ npx webpack ...
I've updated bash to version 4 on Mac OS X Yosemite using homebrew, in order to make it possible to use "globstar" in my npm scripts. E.g. recursively list all .js files in test directory using the pattern ./test/**/*.js. However, npm will run scripts with older bash version (version 3).
Is it possible to make npm uses the updated bash version?
(I'm not sure, but I guess npm uses /bin/bash instead of /usr/local/bin/bash)
UPDATE: My particular use case is to investigate the possibilities to use npm as a build tool, and for instance bundle all test specs with browserify using a npm script such as "build:test:scripts": "browserify ./test/**/*.js -o /build/test-bundle.js",.
Try wrapping the "globstar" with single quotes:
"build:test:scripts": "browserify './test/**/*.js' -o /build/test-bundle.js",
Simply set the script-shell config key to the path of your Bash 4 installation.
script-shell: The shell to use for scripts run with the npm run command.
# local
npm config set script-shell "/usr/local/bin/bash"
# global
npm config set -g script-shell "/usr/local/bin/bash"
I just get started on nodejs. I have installed nodejs and npm. Now, I want to install some packages like mongodb and express. As my default directory path in cmd is C:\>Users\administrator, do I need to make current folder as nodejs folder to run npm install express/coffee-script or I can just run this command under the default directory path mentioned above?
By the way, I always see the npm install command provided by others starts with a dollar sign, but I can only use the command without the dollar sign. So what does the dollar sign stand for?
By default, npm will run in local mode, and install scripts into ./node_modules. This is great if you need to require your scripts, as you'll do with Express.
Calling it with the -g option installs it globally, wherever node is installed (usually, on Linux, in /usr/local. This is great for packages that are meant to be run using the shell (for example, Supervisor).
Generally, if you want to develop a node.js application under C:\foo\bar\myapp, you will run npm from there.
FYI, the $ sign is a general indication meaning that the following command is meant to be run on the command line.