fish shell and global npm modules - npm

I'm experiencing some issues with my local environment.
I'm using:
homebrew (1.1.4)
fish shell (2.4)
n version manager (2.1.0)
node
npm
All of the above were initially installed via homebrew.
Issues:
Any globally installed modules cannot be run.
I cannot update npm versions.
The node versions I use regularly are 0.10.48 and 6.9.2 - but mostly 4.6.2.
I've created some aliases in fish to be able to get the the global modules to run. I'd prefer to address the root cause rather than continue applying further bandaids though.
Extra info:
~> npm config get prefix
/Users/me/.npm-packages
~> npm bin
/Users/me/node_modules/.bin
What happens when I try to update npm:
~> npm install -g npm#3
/Users/me/.npm-packages/bin/npm -> /Users/me/.npm-packages/lib/node_modules/npm/bin/npm-cli.js
npm#3.10.10 /Users/me/.npm-packages/lib/node_modules/npm
~> npm -v
2.15.9
When I try to call a global module:
~> gulp
fish: Unknown command 'gulp'
What I've tried in order to fix the global modules:
~> cat ~/.config/fish/config.fish
set fish_user_paths /Users/me/.npm-packages
ls ~/.config/fish/functions
bower.fish ember.fish pm2.fish
~> cat ~/.config/fish/functions/bower.fish
function bower
/Users/me/.npm-packages/bin/bower $argv;
end
Any help you can provide will be greatly appreciated! I've been trying to fix this for awhile now without any luck!

You can use installed npm modules. You just need to add npm global path to the shell. To do that, I ran:
$ npm config get prefix
// Output: /usr/local/Cellar/node/9.2.1
and then, added to my fish/config.fish:
set PATH /usr/local/Cellar/node/9.2.1/bin $PATH
This is the Github related link that helped me solving the problem.

What happens when you install node application globally https://docs.npmjs.com/getting-started/fixing-npm-permissions
Configuring in OSX 10.12.5 with
fish v2.6.0
node v8.1.0
example command in terminal returns both
term~> npm install jshint -g
/Users/username/npm-global/lib/node_modules/bin/jshint ->
/Users/username/npm-global/lib/node_modules/jshint/bin/jshint
the PATH you should have in shell to enable you to execute jshint (installed package) -> pointing to the actual install location
Include line below for configuring PATH in file ~/.profile
export PATH="$HOME/npm-global/lib/node-modules/:$PATH"

My permanent solution (with macOS Mojave and fish 2.7.1):
set -U fish_user_paths /Users/{USERNAME}/.npm-packages/bin $fish_user_paths
Btw, below one works but only in terminal session:
set PATH $PATH /Users/{USERNAME}/.npm-packages/bin
Note: You can find npm-packages path with:
printf "%s\n" $PATH

For me non of this worked, so i installed oh my fish nvm plugin
omf install nvm

I resolved this by completely uninstalling (and deleting any traces of) node, npm and n, uninstalling fish shell and changing to zsh instead.
Afterwards, I installed nvm and used it to restore node on my machine.
Global npm modules now work.

Related

How to fix 'command not found' for aws-cdk after running the npm install

I am trying to install the aws-cdk and in the terminal and I run the npm install -g aws-cdk. As stated here After npm runs, I get:
/usr/local/Cellar/node/9.8.0/bin/cdk -> /usr/local/Cellar/node/9.8.0/lib/node_modules/aws-cdk/bin/cdk
+ aws-cdk#0.31.0
updated 1 package in 1.636s
If I try to run cdk I get:
zsh: command not found: cdk
Installing as root worked for me:
sudo npm install -g aws-cdk
Make sure /usr/local/Cellar/node/9.8.0/bin is in your PATH
In my case, I added this to the end of my .bash_profile:
export PATH=$PATH:$(npm get prefix)/bin.
I did that based on information from a different thread.
If you're using nvm to manage your node versions, make sure that the CDK package is being installed in the same version of node you're currently using or the version you want to use.
The OP noted their CDK was getting installed in node/9.8.0:
Check the current node version running: nvm current.
If you see that the CDK installation location is different than the node version indicated by nvm current, you'll need to switch your node version using:
nvm use <node-version-where-cdk-is-installed>
In the OP's case, this would be nvm use 9.8.0.
What worked for me in mac was adding to the path the bin directory of globally installed node modules.
Install aws-cdk by:
npm install -g aws-cdk
Try to run it
cdk
no command found error
make or edit your .zshrc file in your user directory
add line to .zshrc with export PATH=$PATH:/{your_user_path}/.npm-global/bin
execute it source .zshrc
now cdk should work
Pay attention that it is mac based approach.

Command not found with vue-cli

While installing the dependencies of vue-cli, vue is not identified. Why?
rm -rf node_modules and npm install again
have a look here
Add sudo before yarn when installing
yarn global remove #vue/cli
sudo yarn global add #vue/cli
vue
I had the same issue for a while.
TL;DR
npm install #vue/cli-service --save-dev
As the documentation specify it is a development dependency https://cli.vuejs.org/guide/#cli-service
The CLI Service (#vue/cli-service) is a development dependency. It's an npm package installed locally into every project created by #vue/cli.
Origin
I had a fresh install of nodejs
And just did
>> sudo npm install -g #vue/cli#latest
>> vue --version
#vue/cli 4.5.8
The issue
The issue presented like this
>> npm run serve
yarn run v1.22.10
$ vue-cli-service build --mode development --watch
/bin/sh: 1: vue-cli-service: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The fixes:
npm install #vue/cli-service --save-dev
Which led me straight to another error message
>> npm run serve
yarn run v1.22.10
$ vue-cli-service build --mode development --watch
ERROR Error: Cannot find module 'vue-template-compiler/package.json'
Which I fixed the same way
npm i vue-template-compiler --save-dev
And now it is working fine.
Installing current version without permanently installing vue-cli.
npx #vue/cli create appname
It shows the vue executable is located at /home/alisha/.local/bin. So probably this location is not there in your $PATH.
You should be able to run the vue commands if you provide the full path, like:
~/.local/bin/vue create hello-world
You can also see if that directory is in your PATH by running some command like:
echo $PATH | grep '.local/bin/'
If it's there, you would see it, otherwise you can add it to your path by placing it in your ~/.profile.
Edit ~/.profile and add the following at the bottom of it.
PATH="$HOME/.local/bin:$PATH"
Hope it helps!!
I had the same issue while making a build for production.
You will require vue-cli to be installed. Use below command to install the latest version.
npm install -g #vue/cli#latest
Then
npm install
Might have to do with you having an old version on your computer:
Warning regarding Previous Versions
The package name changed from vue-cli to #vue/cli. If you have the previous vue-cli (1.x or 2.x) package installed globally, you need to uninstall it first with
npm uninstall vue-cli -g or yarn global remove vue-cli.
You can find it here: https://cli.vuejs.org/guide/installation.html
THIS FIXED THE ISSUE FOR ME:
After running
sudo npm install -g #vue/cli
I ran
sudo nano $HOME/.profile
and pasted the following line
export PATH=$PATH:/home/chike/.npm-global/bin
after writing the code, next thing I did was Ctrl + O, ENTER and Ctrl + X then wrote
vue init webpack myapp
When you install vue using cli that time you got the path of vue.Now you can copy the bin folder path.
In my example /home/sublime/.npm-packages/bin
Now you export the path below command
export PATH=$PATH:/home/sublime/.npm-packages/bin
I solved mine by running (add sudo if needed)
npm i -g vue-cli#2.9.6
npm i -g #vue/cli
Got similar issue when deploy vue project in jenkins.
Here is what I did:
Add node's bin/ dir, to jenkins user's .bashrc file.
e.g
# node
NODE_HOME=/home/dev/.nvm/versions/node/default
PATH=$NODE_HOME/bin:$PATH
Tips - about nvm & yarn
When you manage node version via nvm, make sure you already choose the node version in terminal. e.g:
nvm use stable
node -v
If you use nvm, and installed yarn via npm, then better install vue-cli via npm not yarn, otherwise the vue executable is not placed into node's bin/ dir, at least that's the case in my tests, and as a result will cause you fail to find the vue command.
Using Yarn on Ubuntu it is installed to ~/.npm-packages/bin/. You must add this directory to your PATH. For example run the following command, close your terminal and open a new one.
user#machine:~$ echo 'export PATH="$PATH:~/.npm-packages/bin/"' >> ~/.bashrc
Note: if the file ~/.bashrc does not exist then simply create it.
Following worked for me:
First remove all the existing ones:
yarn global remove #vue/cli
yarn global remove #vue/cli-service
sudo yarn global remove #vue/cli
sudo yarn global remove #vue/cli-service
Then add #vue/cli using sudo:
Note: use sudo if required
yarn global add #vue/cli
yarn global add #vue/cli-service
Then, the final thing to do is to RESTART the terminal.
vue --version
#vue/cli 4.5.9
If you already got a project, the only two things you need to do is:
Delete the directory node_modules (it is safe, since it is not under git and will regenerate in the next step)
In the command-line write yarn install (it will install everything you need)
If you start installing vue-cli manually in a existing project, it the package.json and package-lock.json will be updated. If you already did. Do a checkout from git, and follow my steps above
This may be a problem caused by version conflicts. "export PATH=$PATH:" This is really useful in some cases. But if you are also like me, after trying the direct “export path” method in the comment above, restarting the terminal still can not execute the situation, you can try this way.
Uninstall Vue
npm uninstall -g #vue/cli
Check the local-global npm package installation path, vue is installed in this directory, check if it has been removed.
npm root -g
Install vue (you can check with https://cli.vuejs.org/#getting-started to find the latest command)
npm install -g #vue/cli
Create a connection to the /usr/local/bin directory(You need to find the vue.js path after the local installation first, then replace this path with your latest local install path:/Users/xxxxx/.npm-global/lib/node_modules/#vue/cli/bin/vue.js)
ln -s /Users/xxxxx/.npm-global/lib/node_modules/#vue/cli/bin/vue.js /usr/local/bin/vue
View version number
vue -v
I was getting the same error because Node.js was not installed. My issue got resolved by installing Node.js using the following command:
sudo apt install nodejs-legacy
To see if you already have Node.js and npm installed and check the installed version, run the following commands:
node -v
npm -v
If both are installed then follow the steps here:
https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
I installed the package using yarn global add #vue/cli on my Ubuntu box and found the binary in /home/vonkad/.yarn/bin.
I had to modify my /home/vonkad/.bashrc and add the directory to the path export PATH=$PATH:/home/vonkad/.yarn/bin.
To fix this situation, I had to add the following line to my .zshrc (maybe in your case is .bashrc)
export PATH="$(yarn global bin):$PATH"
Effectively, the yarn global bin is a folder where vue (vue-cli 3) was placed.
What helped me
mac os catalina with zsh terminal
Uninstalled node and npm using https://www.positronx.io/how-to-uninstall-node-js-and-npm-from-macos/
Downloaded node/npm from https://nodejs.org/en/download/current/
sudo npm install -g #vue/cli
vue --version (#vue/cli 4.5.4)
I faced the same issue and now resolved. In my case I installed Node.js and NPM using the default Ubuntu repository by using this command sudo apt-get install nodejs npm
The problems seemed like those 2 packages are not well maintained so it caused some bugs.
So I purge those packages and reinstall it from nodesource which is officially recommended way to install (reference: Installation instruction from nodesource) using these commands.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
Then reinstall #vue/cli again
sudo npm install -g #vue/cli
Now these issues have gone. Hope it helps some programmers.
You need to install vue via sudo like explained in the doc:
https://cli.vuejs.org/guide/installation.html
If you have WSL2 running and you use zsh like me, just add
yarn global add #vue/cli
# add this line to ~/.zshrc
export PATH="$HOME/.yarn/bin:$PATH"
$ vue --version
#vue/cli 4.x.xx
You can try the following code install see
npm install --global vue-cli
vue init webpack <YOUR-PROJECT-NAME-HERE>
cd <YOUR-PROJECT-NAME-HERE>
npm install
npm run dev

Npm updates sucessfully, but still shows old version

When I do a npm -v I see that I am still on 5.6.0. And keep getting prompts to do a npm i npm.
But I've done a sudo npm install npm#latest -g and double checked with a npm outdated -g --depth=0 to see that my global npm is at the latest already (6.0.0).
How do I resolve this conflict? Is the npm I'm using the latest or is it still the old one? Is it because my global is linked incorrectly or something?
Try closing the Terminal - once you open it again it should work, worked for me well.
*IvanD wrote this solution in a comment to the original post, I thought it's worth an answer post.
I can 100% vouch this works.
I got this from someone who teaches node.js.
Give NVM (Node Version Manager) a try. Do the commands in the following ordrer:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
command -v nvm
nvm install node
finally confirm that the update was successful
npm -v
Let's do a better solution if all of the mention methods not help you:
You need to install nvm (Node Version Manager)
After that just run:
nvm install 14
list your versions to see is version installed
nvm ls
and you will get list like this:
v10.2.0
v12.18.3
-> v14.10.0
system
default -> 14.10.0 (-> v14.10.0)
node -> stable (-> v14.10.0) (default)
stable -> 14.10 (-> v14.10.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/erbium (-> v12.18.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.22.0 (-> N/A)
lts/erbium -> v12.18.3
Switch default version like this:
nvm alias default 14.10.0
And check:
node -v
If print v14.10.0 you are ready to rock, if not:
nvm use 14
and check again.
This need to work like a charm and you will be happy.
Check your /usr/local/lib folder. I had the same problem and a node_modules folder in here. After removing it and rerunning [sudo] npm install npm#latest -g it showed and used the correct version.
sudo npm install -g n
If you want latest stable version
sudo n stable
If you want the latest version
sudo n latest
Then restart computer
I had this issue today with Node installer node-v10.16.0-x64.msi. Even after installing it I got the node version from npm -v as 3.10... and my react app would fail to start :(
It seems that node creates a folder in APPDATA at %AppData%\Roaming\npm
There was an old npm.exe lying there and this was in my Windows Path at user level. The user level Path overrides the system level Path where the new installer had added its path. The user override caused the new npm to not be found. Solution: Shift-Delete %AppData%\Roaming\npm ! :D Remove and reinstall with node-v10.16.0-x64.msi
Now npm -v gives 6.9.0!
If you have tried all the mentioned approach like :-
sudo npm install -g n
sudo n stable
or
sudo npm install npm#latest -g
still npm -v showing you the older version, once try to close your terminal and open a new one and check.
I found this command line that worked for me.
Update from version 12.18 to the latest version 15.2 correctly.
I hope this is useful:
nvm install node --reinstall-packages-from=node
I think your question is replecated and already has an answer.
First, The LTS and current version of NPM is 5.6.0 for both Node 8.11.1 (LTS) and Node 10.0.0 (Latest current version) according to the official documentation.
Otherwise, sudo npm install npm#latest -g should install the latest version NPM globally.
Search for a node_modules folder in your home directory, rename it or delete it, then try again.
First uninstall your installed nodejs from Control Panel\All Control Panel Items\Programs and Features.
Delete nodejs folder from the Program Files (x86) folder.
Now install new version and check it once.
Though this is an old question and the answer might not be relevant but I found myself into such situation many time, sometimes restarting the server is not a solution because other process will get effected. So you need to refresh the terminal without actually restarting the server and for that you may run exec bash and that will certainly help.
I was facing the same error today.Updated npm version does not reflect.
This is how I resolved it.
A lot of post suggested to use the command npm cache clean --f. But, it did not work for me. Hence, I deleted the "npm" and "npm-cache" folders from the location C:\Users\SearchYourUserName\AppData\Roaming
Uninstalled the existing node.
Re-Installed node version v10.x.x. I downloaded the node-v10.22.1-x64.msi file from https://nodejs.org/dist/latest-v10.x/ and installed it.
Then checked the npm version. It had worked.
Actually this could be happening due to mistakes like you installing packages using yarn ex:
// update the version number in package.json then
yarn install
and trying to run scripts using npm like
npm run start
Actually I did this mistake.
So make sure to use either yarn or npm, but not both.
I faced a similar issue. I had node v14.15.4 installed on ubuntu 18, but when i used node -v it showed v11.0.0.
To check all the node versions available on your system using nvm, use
nvm ls
To set the newly installed node version as the default, use
nvm alias default <version>
Then, just close the terminal and open a new terminal to check the node version.
Problem summary
npm -v # 6.xx
npm install npm#latest -g
npm -v # still the same, but should be 7.xx
Solution based on the answer of fypnlp.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
The terminal now wants to configure some ~/.zshrc file which I don't have on my Mac yet. So let’s create it
nano ~/.zshrc
and paste
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Now restart the terminal and run
command -v nvm # should return "nvm"
nvm install node
npm -v # now it’s 7.xx yay!
I have this same symptom but none of the answers helped and I finally found the issue.
It may not be exactly the same as the OP's but I think it is along the same lines. #Kelvin Zhao's solution of un-installing and re-installing worked for him but not for me so this might be useful for others.
My issue ended up being that I use brew and (I can't remember how exactly) but I seem to be have an old version in the brew Celler.
Brew didn't know about it, or couldn't tell me as I do a brew uninstall node and it says its doesn't exist.
When I run node -v or npm -v I get the old versions.
I uninstalled and reinstalled using brew and npm and sudo npm etc didn't do it. it showed the correct versions being installed but node -v still showed the old.
Turns out there is a version in my /usr/local/Celler directory named node#14.
So running brew uninstall node#14 got rid of that and now node -v doesn't work anymore.
Now re-installing node works. brew install node
More info for further details for those interested.
I wanted to find out where I still had node installed so I did echo $PATH as it had to be somewhere in the path else node -v wouldn't be recognised.
Turns out I had /usr/local/opt/node#14 in my path (which linked to /usr/local/Celler/node#14)
That is how I found it.
And now I think I remember how it got there. I'm 90% sure that I did an upgrade of node and brew asked what to do with the old version and I think I said to keep it.
And that was my mistake that lead me here (and where I think OP's situation differs)
In my case, I wanted to use Version 18 and it was showing 16. I'm using homebrew on my mac and the command that worked for me was the following:
brew uninstall node#16
And voila the version that was showing after is 18.
I had issue with VS Code when I did nvm alias in its own terminal, even after ran alias command, the version kept switching back to old version even I open a new terminal within the VS Code. But as stupid as it sounds, close and reopen the vscode for me worked, after I set the version in an independent terminal.

Bash: cordova: command not found on mac

I used command sudo npm install -g cordova to install cordova it shows result in terminal/Users/paramesh/.npm-packages/bin/cordova -> /Users/paramesh/.npm-packages/lib/node_modules/cordova/bin/cordova
+ cordova#8.0.0
updated 1 package in 47.732s
while creating project in cordova its bash: cordova not found
In my case, the path of npm was not set correctly. The package was installed on my local path even if I install it globally.
First, check npm path using npm root
If its not /usr/local/lib/node_modules then
set your path using npm config set prefix /usr/local
and again install you package npm install -g cordova
It's strange though that your system cannot find cordova after a global install with sudo. Have you tried reinstalling? If this doesn't help, try the following.
Restart the terminal and type this to check whether your system can find the cordova executable:
which cordova
If you get no output, it means your system cannot find the executable cordova binary. UNIX systems require executable binaries to be placed within the PATH environment variable. Try adding the following line in your ~/.bash_profile file:
$PATH=$PATH:/Users/paramesh/.npm-packages/lib/node_modules/cordova/bin/cordova
If you don't have a ~/.bash_profile, add it to the ~/.bashrc or ~/.profile file, whichever exists already. Then within the terminal you want to call cordova from, type:
source ~/.bash_profile
which cordova
This should give you the location of the cordova binary. Now you can use it.

npm installs each package twice

I have just installed Homebrew and then I have installed Node (via $ brew install node). When node was installed, npm was automatically installed too (why?).
anyway, now when I install any package from npm, for example $ npm install -g express - it is installed in two locations:
Users/myuser/.npm/express and usr/local/lib/node_modules/express
$ which express retrieves me usr/local/bin/express which refers to usr/local/lib/node_modules/express. I think it's ok that it's installed inside usr/local/lib/node_modules/.. because I used -g flag; but why it is also installed under my own user (Users/myuser/.npm)?
In addition, how does the terminal know about express command? I didn't define any .bash_profile file. how does it know to go to usr/local/bin/$PATH?
Node installs NPM also because Node without NPM wouldn't be Node! :)
NPM is all about a succesful registry. You wouldn't go much further without it.
~/.npm is a cache for all packages - so this actually isn't an installation. As you may know about caches, this is just to avoid re-downloading things over time.
Also, this is configurable via the cache config.
Finally, the last location - usr/local/lib/node_modules is the actual global installation of Express.