node user directory leading to command not found - node.js

I suipidly ran this script to stop having to use sudo on npm -g commands and now my node_modules are located at:
/Users/myusername/.npm-packages/lib/node_modules
when trying to run a commands i.e yo bower i get -bash: bower: command not found
Do I need to link this directory to the usr/bin?
In trying to sort this out I have run lots of commands and created symbolic links, I feel like my system is a real mess and I cannot really see what is going on? Help to ensure my system is clean would be really helpful.

Generally, it's a good idea to ensure that you do not need admin rights to run npm commands. The problem you're having, is that the directory where npm now installs its commmands (/Users/myusername/.npm-packges/bin) is not in your PATH. Your PATH is a list of directories where your shell searches for the commands you type.
You can add the directory to your path by adding the following line to the .bashrc file in your home directory (create it if it doesn't exist).
export PATH="${PATH}:/Users/myusername/.npm-packages/bin"
Open a new terminal, and bower should work again.
As an additional tip, I believe you're running on OS X? When installing Node.js using Homebrew, it is automatically installed in a proper way (no sudo needed to install packages globally and they will just work). Next time you're installing Node.js (or something else), Homebrew might be worth a try.

Related

Where is the npm directory supposed to be?

The Node version is 17.7.1. When I run npm -v I get:
bash: /usr/local/bin/npm: No such file or directory
But when I run npm root -g I get:
/usr/local/lib/node_modules
Running npm list -g also gives me the same message as running npm -v.
So, where is it supposed to be and how can I fix this?
There are a few issues as to why this is occuring.
It appears that your computer cannot find the following path (below) to the npm folder, but it can find the global node_modules folder.
/usr/local/bin/npm
This may occur for a number of reasons. Luckily, there are many solutions to these issues.
1. Reinstall Node.js and npm^
The first solution is pretty obvious, and it is to reinstall Node.js and npm^.
The installation process is very simple.
If you are using only the command line, you can install Node.js using the following command. The npm command comes installed with Node.js.
$ sudo apt-get install nodejs
Or, if you want to use the installer, download and run the installer from nodejs.org.
Check the versions of Node.js and npm to see if it has worked.
$ node -v
$ npm -v
The helpful thing about reinstalling Node.js and npm in this case is that, if Node.js and npm weren't installed in the first place (highly unlikely), it will install them.
However, if they were already installed, but then something happened to the installation directory (e.g. it was renamed, moved, deleted or corrupted), then the new installation will most likely have fixed that issue.
2. Add /usr/local/bin/npm to the path (solution worked for OP)
If the first solution didn't work for you, then the next issue that might be occurring might be that /usr/local/bin/npm isn't in your path, which means that bash cannot find it.
To add npm to your path, follow the steps below.
Normally, to get the path with npm, you need to run the following command.
$ npm bin
Obviously, we have to use a workaround. But, we can just use an assumed path for where the binaries of npm are stored, which is...
/usr/local/bin/npm/bin
However, this may vary, so, to be sure that this exists, you can simply type it into the command line.
$ cd /usr/local/bin/npm
Then, you can run the ls command, and see if there are any bin folders or .sh shell scripts that might be used for the path.
$ ls
Make sure you save the binary part for later!
Open the home directory using the following command. This is because the .bashrc file is in the home directory (used in the next step).
$ cd $HOME
Open the .bashrc file using vi (or any file editor of your choice, but vi will be used in this example).
$ vi .bashrc
Then, press the i key for Insert Mode. Type the following in the file.
export PATH=/usr/local/bin/npm/bin:$PATH
In vi, press Esc, then press the following keys: :, w, q, !, Enter.
:wq!
The file is now saved and the editor is closed.
Normally, the .bashrc file is only read when you log in each time. To avoid doing that, you can force bash to read the .bashrc file and update itself. To do this, run the following command.
$ source .bashrc
The bash shell should now be restarted.
These were quite a few steps (four, but many were compressed into one), but if a situation occurred where for some reason npm wasn't adding itself to the path, this solution should fix it.
3. Denied read/write access
Another issue is that your user profile doesn't have the right permissions to read/write to usr/bin/local/npm, or you didn't give npm permissions.
The fix to this solution is very simple.
Run the npm commands you want to run using sudo.
$ sudo npm -v
$ sudo npm list -g
If you want, you can make the commands you run automatically run with sudo.
$ sudo -s
$ npm -v
$ npm list -g
Warning: If you don't have the password for sudo, you can't run any of the commands using sudo above.
This should fix your issue, if denied read/write access is the problem.
There are most likely many more solutions^^, but they might be added later.
^ This is a very usual process, but it has to be added here so that I can be sure that this solution has been tried.
^^ These solutions may be found later and edited to this answer. Edited once.
use this command :
npm list -g
you should see your npm directory at first line

Run npm as superuser, it isn't a good idea?

I'm getting errors with npm while trying to install/update packages without SU permissions on Linux.
The easy way to solve the problem is execute sudo npm install <package>, but I'm not sure if it is a good idea.
Best way is to become the owner of .npm folder, as I found into StackOverflow's questions and blog posts.
My question is: why run npm as SU it isn't a good idea?
Running npm as a super user has a risk of running some untrusted code as a super user which can potentially mess with your entire system. Running npm as an unprivileged user has a risk of running that code with less privileges and it won't be able to mess with the entire system - just with your own files (which can be equally bad, depending on how you look at it).
What I often do and recommend is to install Node in your home directory instead of globally on the system if it's your own computer. That way you don't have to run with sudo or su for npm or even for make install of Node itself.
I run a lot of versions of Node that I compile from sources sometimes with different switches and the convention that I use is to install Node in versioned directories, either globally in /opt (but then you need sudo) or locally in my home directory in ~/opt.
I do it like this:
wget https://nodejs.org/dist/v7.1.0/node-v7.1.0.tar.gz
tar xzvf node-v7.1.0.tar.gz
cd node-v7.1.0
./configure --prefix=$HOME/opt/node-v7.1.0
make && make test && make install
Then I create a symlink ~/opt/node pointing to ~/opt/node-v7.1.0 and I have:
PATH="$HOME/opt/node/bin:$PATH"
in my .profile or .bashrc.
That way I don't have to run as super user for installing Node or for running npm.
As a bonus I can quickly switch my default Node version just by changing the symlink, and at any time I can run any other version if I change the PATH or run Node with a full path like ~/opt/node-v7.0.0/bin/node.
I explained that installation process in more detail in my other answers:
node 5.5.0 already installed but node -v fetches with “v4.2.1” on OS X & homebrew?
NodeJS API with external deps in other language
I don't want to go into too much detail here since this answer is about why running npm as a superuser is not a good idea - this installation process is just one solution to not have to run npm as a superuser.
Other options of setting your npm permissions to avoid running as a superuser are described in Fixing npm permissions in npm docs (thanks to RyanZim for pointing it out in the comments).

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.

NPM throws errors at traceur install on OS X

https://github.com/tejas-manohar/itnerary-civic-hacking -- I cloned this git repo down locally. Installed grunt-cli globally, and ran npm run nss (script written by author of original server template -- look in package.json, not complex) and faced numerous errors at the traceur install + git cloning. I've included the trail from terminal window in the pastebin linked below. The npm debug/error log mentioned is not present at the suggested location. npm install alone does not help the situation and returns nothing back. NodeJS v0.10.29 is installed via Node Version Manager (NVM) on OS X 10.9.3.
http://pastebin.com/UJFL3k2E
I'm not the most adept with some of the technologies discussed here. Would someone please attempt to walk me through (a) solution(s) and/or steps to get better clues? All assistance is appreciated.
There's a couple of problems going on which are causing the problems you have seen. The first of which is the command that is run when you run npm run nss, which fails for you with this:
rm: ../../app/static/js/vendor/traceur.js: No such file or directory
This is because, well, the file simply does not exist (yet). If you look at the commands which are run when you run npm run nss, you'll see that the remove file is followed by the copy file of traceur.js to that very location. So I would guess that whoever wrote the script intended for this to be run after it was initially setup, and the remove/copy would be done once the file was initially copied there. Therefore, you must first copy the traceur.js file to app/static/js/vendor before you can successfully execute npm run nss.
But then the next problem, the file should be copied from tools/traceur-compiler/bin doesn't exist either. This is because the traceur-compiler project has recently deleted this (compiled) file from their project, which you can see via this commit: https://github.com/google/traceur-compiler/commit/429c3d850dcb7636320e81fd782c61a06de0fbf1
So you need to regenerate this file, which you can do by (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor
This will get you in the same state as if you successfully ran npm run nss.
You won't be able to re-run the npm run nss command, but you really shouldn't need to since its more of a setup command than anything (it creates a directory, clones a github repo, etc). If you wanted to update the traceur-compiler in the future (though you may never have to do this) then I would recommend running the following steps (from the itnerary-civic-hacking root directory):
cd tools/traceur-compiler
git pull origin master
npm install
make bin/traceur.js
cp bin/traceur.js ../../app/static/js/vendor

yeoman permission issue with nodejs and angular

I am trying to start an angular web app with yeoman but I get permission issue when trying to install the new generator. I can bypass and install generator with sudo but then I get the permission errors when running
**yo** angular
I deduce its because its trying to access npm modules that are global which the current user doesnt have permissions for, and I cant run Yo with sudo. I have done a lot of google searches and they all involve some type of hack with the NODE_PATH in the .bashrc or moving the node modules to the home directory. Has anyone found a simple solution for this issue.
Below is my problem in screenshots:
yo installs fine
when i try to install the generator without sudo complains..
install with sudo passes.
then finally when I try to run yo angular it breaks.. I believe its because yo runs as user and I have installed everything with sudo privileges. How can I get past this?
The reason it breaks, I guess, is because the whole directory tree was created as super-user.
The hacks you mentioned about using NODE_PATH and the home directory are not hacks. They exist for this same very reason. To tell node where to look for packages. And .bashrc is the place where you are supposed to change this kind shell variables.
Say you added ~/.node_modules to you NODE_PATH, then you can install all "global" in there. You could also change the permissions on /usr/local. But on linux world that is not recommended.
I also strongly recommend in not using global install with npm. Using -g and npm link is handy when developing but you shouldn't count on them. You can introduce subtle bugs in your code when you forget to add a package on you package.json but it is installed globally.
Instead of installing it globally, you can find all the packages executable on ./node_modules/.bin/ directory.
But lets say you don't want to be typing ./node_modules/.bin/yo all the time, you could create an alias on your .bashrc.
alias yo="$PWD/node_modules/.bin/yo"
and it would work like expected, and if there is no yo package installed, you get an error.

Resources