'How to install a global npm package using fnm (Fast Node Manager)?

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: enter image description here

And when checking the global package list, nodemon seems to be there: enter image description here

But when running the command nodemon I get the following output: Image of my terminal output error

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.



Solution 1:[1]

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:

  1. Go to C:\Users\<YOUR_USER>\AppData\Local\fnm_multishells and delete the directory if it exists.

  2. 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valchy