How to install a global npm package using fnm (Fast Node Manager)? - node.js

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.

Related

Express command not found after setting installing globally

When I run sudo npm install -g express-generator:
/Users/myname/npm/bin/express -> /Users/myname/npm/lib/node_modules/express-generator/bin/express-cli.js
/Users/myname/npm/lib
└── express-generator#4.15.5
And when I run express:
express not found
Also, I thought it was interesting that when I run which npm:
/usr/local/bin/npm
That is the path.
It seems that whenever I try to install something like yeoman or this express generator globally, it never works.
It should also be noted that I am on an Mac running zsh.
Most likely your npm bin directory is not in the path. Try to list the files in that directory by ls -l /usr/local/share/npm/bin/.
If you find the express file, you can add that directory to the path by export PATH=/usr/local/share/npm/bin:$PATH. If not, most likely something went wrong with your installation and you can try installing the module again.
Running this in the command line:
export PATH=/usr/local/bin/npm:$PATH
Fixed the issue...for now. I'm not totally sure why that worked.
This affects Homebrew users using Node.js & npm
When you install Node.js using Homebrew it does not put npm on the PATH for you, however it suggests that you should do so.
Homebrew doesn't modify the user's environment or dotfiles. However, brew install node does print a message suggesting the user add that path to their PATH.
Source

Installing nodejs and npm on MSYS2

My OS is win7, and I using MSYS2(version:MSYS_NT-6.1), Please give advice how to install nodejs and npm on this terminal, Thanks!
I found a solution for resolving the problem,
64bit env.
pacman -S mingw-w64-x86_64-nodejs
32bit env.
pacman -S mingw-w64-i686-nodejs
after installed, Open terminal
$ node -v
v6.11.0
As of 2020, the package mingw-w64-x86_64-nodejs is not available any more. The simplest way to have Node.js, npm and git installed on a Windows machine is using the official Windows installers:
Git: https://git-scm.com/download/win
Node.js (npm is shipped with it): https://nodejs.org/en/download/
After installation, open a command prompt (click on start, and then type cmd and [ENTER]) and verify that all three tools are there:
git --version
node --version
npm --version
Later on, to update Node.js, simply reinstall it from the same source.
I wasted a lot of time on this. My solution is:
Download the Windows Binary (.zip) from nodejs site (https://nodejs.org/en/download/current/)
Extract it to some folder
Add that folder to the PATH env variable
It does work to use the Windows installer, and Node.js helpfully provides bash-script versions of npm and npx in C:\Program Files\nodejs\ to help streamline the process.
However, contrary to Cerclanism's comment # jmgonet's answer, you should not use --full-path with MinGW, no matter what terminal you're using, since that will by default bring the entire Windows path into your MinGW environment.
(Assuming you're a typical Windows developer with things like MSVC, Windows Python, and etc. install dirs on your path, containing plenty of names that clash with MinGW path members, you can see how that might bite you at some point down the road. My full Windows CMD.exe %PATH% is 1236 characters! I don't want all that sucked into MinGW.)
Instead, you should add the nodejs install dir to your MinGW shell $PATH, say by using everyone's favorite ~/.profile/~/.zprofile $PATH-munging trick:
# Append node.js to path
case ${PATH} in
*"/c/program files/nodejs"*)
;;
*)
export PATH="$PATH:/c/program files/nodejs:"
;;
esac
You'll probably also want to set some configuration, since by default Windows npm will use ${APPDATA}/npm for prefix, ${LOCALAPPDATA}/npm-cache for cache, C:\Windows\system32\cmd.exe for shell, etc.
# To view the full config including all defaults and overrides
npm config ls -l
# To view the active config for the specified environment
npm config list -L {global,user,project}
Maybe I was just confused, but to me it seemed, from what the configs show/say, that setting prefix= in my user config would override even local installs. (The project-specific ones where you npm install without --global, directly into a node_modules subdir of the current dir.) But after testing, happily I can report that's not the case, so it's safe to override the builtin prefix= from your $HOME/.npmrc.
Whether or not you move the cache= or let it stay at C:\Users\<you>\AppData\Local\npm-cache\ is your call. I'm sure it'll work that way. (Well, maybe not from an MSYS shell, but from MinGW it should be fine.)
There are minor differences I haven't overcome, but the only one that comes to mind right now is:
npm help <command> opens a browser window to HTML documentation, instead of displaying man page content directly in the terminal like it does on Linux. (Makes sense, as I don't think the manpages are even installed on Windows. Still disconcerting, though.)
You can simply install nvm, then install nodejs through there. In your MSYS2 shell just run the following to download and install nvm. Its better to go directly here and copy the download commands as the version numbers will change in the url.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
then run the following to setup nvm on your bash path:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
After running those commands you can use nvm install 16 or whatever node major version number you want. Type just nvm to get a list of available commands.

'gulp' is not recognized as an internal or external command

I am trying to use Gulp and Node.Js to stream my process for minifying and concatenating CSS/JS files for production.
Here is what I have done.
I installed Node.Js on my Windows 7 machine.
Installed Gulp globally using this command npm install -g gulp
Then I added a new User variable called NODE_PATH set it to %AppData%\npm\node_modules
After closing and re-opening my command line, I tried to run a gulp task from the command line (i.e. gulp css). But that give me the following error
'gulp' is not recognized as an internal or external command
How can I get this to work?
When I opened the following path using the search bar in windows,
%AppData%\npm\node_modules I see the following two folders
gulp
gulp-cli
I've tried to add %AppData%\npm\node_modules to the Path variable on my system variable, but it did not take it because that variable reached it's max allowed character limit.
I removed couple of paths from my Path variable and added ;C:\Users\[MyWindowsUserName]\AppData\Roaming\npm\node_modules
but still not working.
I even tried to set the path at run time using this command
PATH=%PATH%;C:\Users\[MyWindowsUserName]\AppData\Roaming\npm\node_modules
then run gulp and still the same issues.
What am I missing here? What else do I need in order to get this to work?
I had similar issue when I installed locally initially(w/o -g). I reinstalled with -g (global) and then it worked.
npm install -g gulp
you should run gulp from folder where gulpfile.js is available.
I solved the problem by uninstalling NodeJs and gulp then re-installing both again.
To install gulp globally I executed the following command
npm install -g gulp
Go to My Computer>Properties>Advance System Settings>Environment Variables>
Under the variables of Administrator edit the PATH variable & change its value to "C:\Users\Username\AppData\Roaming\npm". Note: The username in the path will be the current Admin user's name that you have logged in with.
I had the same problem on windows 7. You must edit your path system variable manually.
Go to START -> edit the system environment variables -> Environment variables -> in system part find variables "Path" -> edit -> add new path after ";" to your file gulp.cmd directory some like ';C:\Users\YOURUSERNAME\AppData\Roaming\npm' -> click ok and close these windows -> restart your CLI -> enjoy
You may need to install the gulp-cli globally. Uninstall then re-install if you already have it:
npm uninstall -g gulp-cli
npm install -g gulp-cli
Sorry that was a typo. You can either add node_modules to the end of your user's global path variable, or maybe check the permissions associated with that folder (node _modules). The error doesn't seem like the last case, but I've encountered problems similar to yours. I find the first solution enough for most cases. Just go to environment variables and add the path to node_modules to the last part of your user's path variable. Note I'm saying user and not system.
Just add a semicolon to the end of the variable declaration and add the static path to your node_module folder. ( Ex c:\path\to\node_module)
Alternatively you could:
In your CMD
PATH=%PATH%;C:\\path\to\node_module
EDIT
The last solution will work as long as you don't close your CMD. So, use the first solution for a permanent change.
In my case, this problem occured because I did npm install with another system user in my project folder before. Gulp was already installed globally. After deleting folder /node_modules/ in my project, and running npm install with the current user, it worked.
You need to make sure, when you run command (install npm -g gulp), it will create install gulp on C:\ directory.
that directory should match with whatver npm path variable set in your java path.
just run path from command prompt, and verify this. if not, change your java class path variable wherever you gulp is instaled.
It should work.
If you have mysql install in your windows 10 try uninstall every myqsl app from your computer. Its work for me. exactly when i installed the mysql in my computer gulp command and some other commands stop working and then i have tried everything but not nothing worked for me.
I just encountered this on Windows 10 and the latest NodeJS (14.15.1). In my case our admins have our profiles and true "home" folder remotely mount onto our work machine(s). Npm wanted to put its cache over on the remote server and that has worked until this release.
I was unaware that npm has a .npmrc file available. I added one to my actual machine's C:\Users\my-id folder and it contains:
prefix=C:\Users\my-id\nodejs\npm
cache=c:\Users\my-id\nodejs\npm-cache
I also added these paths to my PATH environment variable.
I went to the APPDATA folder on my work machine and the remote "home" server and deleted all the npm related Roaming folders. I deleted the node_modules folder in my project.
I closed all open windows and reopened them. I brought up a command prompt in my project dir and re inited npm and reinstalled the modules I wanted.
After that everything is rolling along.
Just to add to #Vinod R's answer
Go to My Computer>Properties>Advance System Settings>Environment Variables>
Under the variables of Administrator edit the PATH variable & change its value to "C:\Users\Username\AppData\Roaming\npm". Note: The username in the path will be the current Admin user's name that you have logged in with.
After this, move the path to the top as shown in the image attached.
image for demostration
I was having the same exception with node v12.13.1,
Downgraded node to v10.15.3 and it works fine now.
The best solution, you can manage the multiple node versions using nvm installer. then, install the required node's version using below command
nvm install version
Use below command as a working node with mentioned version alone
nvm use version
now, you can use any version node without uninstalling previous installed node.

BrowserSync : Command not found after installing browser-sync

There was another issue open with the same title which has been closed but my error could not be resolved using the solutions given in that issue.
Mac OSX El Capitan
Node [v5.6.0]
Npm [v3.6.0]
I installed Browser-Sync successfully using :
bash
$ sudo npm install -g browser-sync
but when I check the browser-sync version by typing :
bash
$ browser-sync --version
I get this error :
bash
-bash: browser-sync: command not found
Check where npm installs it's binaries by running npm bin -g, and then add that to your PATH.
export PATH=$PATH:/usr/local/bin
None of this was helpful to me, instead what I did was find the file location of browser sync after i executed the browser-sync download command supplied on their website. npm install -g browser-sync
Turns out my windows 10 put that downloaded file into %appdata% folder which is in Local Disk (C), but it's an invisible folder and not the correct place it should be, it needs to be in the nodejs folder inside Program Files...
So to find this folder do this: in the search bar of windows 10 type in %appdata% and press Enter, it should open up that hidden folder...
Then click on npm > npm_modules > then you should see files saying browser-sync. Highlight and drag all those files into Local Disk (C) > Program Files > nodejs. You will get allow access prompts, just allow all those.
Now browser-sync and nodejs are sitting happily together and NOW, you can go back to your CMD window and type in browser-sync --version and get the correct version.
I am taking the Coursera class titled HTMl, CSS, and Javascript for Web Developers and ran into this very problem and this solution made it possible for me to continue setting up my environment to take the class.
Although above answers are correct, there is an easy way to handle this. Instead of explicitly creating $PATH we can install Browsersync globally.
npm i -D browser-sync
Here -g option specifies npm to install browser-sync package globally and add $PATH automatically. Next check wether it is working or not by running:
browser-sync --version
Note: Even if you already installed Browsersync try running this. The issue will be resolved.

Executables commands not found in -bash terminal [duplicate]

This question already has an answer here:
NPM -bash command not found
(1 answer)
Closed 6 years ago.
Using a Mac Os X Yosemite was trying generate some scaffolding and this is what comes back in the bash terminal
-bash: some_command: not found
This happens with most of my commands like run, serve etc when trying to generate or use certain executable commands. General alias’s and commands like mv, ls, rm etc work fine
I’ve checked my $PATH and it seems correct:
$ /bin/echo $PATH
/Users/user_name/.rbenv/shims:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/heroku/bin:/usr/local/bin:/usr/local/sbin:/usr/local/share/npm/bin:/Users/user_name/.rbenv/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin
I checked to make certain npm, node, yomen, grunt etc show as installed with -v and shows updated and installed.
I’ve checked my ~.bash_profile appears and aliases but all seems fine.
The best I can gage is it has something to do possibly with npm and or Node and possibly Homebrew. So I’ve uninstalled, reinstalled and researched any similar like questions out there but nothing seems to work. Hoping someone can point me in the right direction here since I am at a lost. And I am not the best at the environment set up. So might not be looking at something thats staring at me in the face. Thanks in advance.
Make sure to install whatever CLI tool you're trying to use first if you haven't already. For example:
npm install -g yo
That command installs yeoman into the global npm module directory. That directory should also be in your $PATH. To find the directory npm uses, type the following:
npm config get prefix
That will show you what directory npm is installing global modules to. In that directory there should be a bin/ directory with symlinks for any CLI tools that came with a global module you installed. That bin/ directory should be in your $PATH variable for bash to find command line scripts in there.
I'm using NVM to manage my node installations so when I run npm config get prefix my directory is: /Users/chev/.nvm/versions/node/v5.0.0. That's not the standard directory though. If you installed node with the default installer then yours will be different. When I do echo $PATH mine looks like this: /Users/chev/.nvm/versions/node/v5.0.0/bin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin. Directories in path are separated by colons (:).
A common problem with node on OSX is the need to use sudo with npm commands because of permissions issues in the default global npm modules directory. If you're having issues like that I suggest you also start using something like NVM. NVM allows you to install multiple node versions side by side, but even if you don't need that functionality it's still useful because it configures node & npm so that everything operates out of your home directory at ~/.nvm/ and avoids permission issues. No more sudo.

Resources