Express command not found after setting installing globally - node.js

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

Related

-bash: jspm: command not found

I am trying to npm install JSPM, so that I can run jspm init, for instance.
npm install -g jspm
is unfortunately not enabling the jspm cli.
npm install jspm --save-dev
in project root, isn't doing the trick either.
node -v = v6.2.1
npm -v = 3.9.3
echo $PATH = /usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/git/bin:/usr/local/bin:/usr/local/git/bin:/usr/local/bin
The error
-bash: jspm: command not found
occurs after running jspm init, or jspm.
OS: OS X El Capitan version 10.11.3
Symlink when running an
npm install -g jspm
/Users/user/.npm-packages/bin/jspm -> /Users/user/.npm-packages/lib/node_modules/jspm/jspm.js
/Users/user/.npm-packages/lib
My thanks as always :)
You might need to add it to your environment variables.
In windows add the following line on your path environment variables:
%appdata%\npm
In OS X it might be something like (I am not sure about the exact syntax but it might give you some direction):
~/.bash_profile
export PATH=$PATH:.
I fixed this by running the installation as sudo:
npm install jspm -g
A lot of the other posts had elaborate workarounds involving NVM, editing permissions on bash files, etc. but I'm too busy and just need this to work. 😉
In the end after thinking through the answer here is what I did to solve this problem.
ONE
I searched around and found that my $PATH variable was located here ~/.profile
so running
emacs ~/.profile
I was able to edit path directly from my terminal(Iterm).
TWO
I made sure to remove any duplicate filepaths and to make sure that all default paths went first, so that my local paths would override them. It ended up looking like this
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/sbin:/usr/local/git/bin:/usr/local/mysql/bin:/usr/local/bin
THREE
In addition, I realized that if any path was missing it would be the
/Users/user/.npm-packages/bin
filepath.
I added it, where user would be your name. In my case charlie, so
/Users/charlie/.npm-packages/bin.
That did it, running jspm worked as expected. NOICE!
FINAL $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/sbin:/usr/local/git/bin:/usr/local/mysql/bin:/usr/local/bin:/Users/charlie/.npm-packages/bin

How can I get the slc command to work on Ubuntu?

I have installed Strongloop using npm install -g strongloop on my Ubuntu 14.04 server. The slc command does not work. It says
The program 'slc' is currently not installed. You can install it by typing:
sudo apt-get install heimdal-multidev
How can I get it to run the Strongloop CLI instead of looking for this package? I have added this to my PATH and it still doesn't work. Any ideas?
Other Strongloop commands, like sl-build work and strongloop is listed in npm list -g.
Ubuntu 14 with node.js 4.1.2
By default somehow slc is not created or not added to PATH.
I solved this problem by adding symlink:
sudo ln -s /usr/lib/node_modules/strongloop/bin/slc.js /usr/bin/slc
A soft link named slc should have been created at /usr/local/bin which will point to strongloop binary.
Please verify if the following exists.
/usr/local/lib/node_modules/strongloop/bin/slc
If no, then strongloop did not get installed successfully, otherwise verify the existence of the softlink slc at /usr/local/bin/.
/usr/local/bin/slc -> /usr/local/lib/node_modules/strongloop/bin/slc
If yes, then /usr/local/bin needs to be added to the $PATH, otherwise create the softlink and verify that /usr/local/binin $PATH.
Looks like the Node installation that optionally comes with a Digital Ocean Droplet installs to a different location that's not in $PATH. I'm pretty sure that was the issue. Anyways, I fixed it by spinning up a server without Node pre-installed and followed this guide. Just use npm install -g strongloop instead of strong-cli because the latter has been deprecated.
Ubuntu 14.04 with node.js 4.4.2 (LTS) :
The installation of strongloop was done without any errors but slc was not added to the PATH. I solved this problem by adding the symlink:
sudo ln -s /usr/local/lib/node_modules/strongloop/bin/slc.js /usr/bin/slc
Actually i am not sure my case matches with yours but i want to share my experience. i got the same message anyway.
I realized that i had changed prefix of global packets before. Then i checked prefix with the following command.
$ npm config get prefix
/home/myUser/.node_modules_global
Then i added the path to PATH variable (but .profile, .bash_profile files will be better) in active command line window and problem solved.

How to put local node package on path?

Newbie question. I have chosen not to install express with -g option. I did not use npm -g which would put it on the path globally. Instead it is installed in my local mac user directory. What I am not clear on is exactly what or how you put a package like express on the path so it can be invoked etc? What exactly needs to be on the path (node_modules?) so these packages are available just like a -g installation? I could have used home-brew I suppose but anyway, I now have all node packages and everything local. Another situation is that I am not able to run any of the nodejs tutorials. Although there might be smarter ways to do this, I wonder if sudo is really such a good way to install a development package ....
Now for example, I want to run the tutorial javascripting which is a nodejs tutorial. How do I do this. If I just type:
Mac1$ javascripting
it finds nothing.
Same for
Mac1$ express
UPDATE: THIS WAS ANSWERED IN THE COMMENTS
The commands exist in a hidden directory after a regular
install npm install express
in my case this the command goes here: /users/MAC1/node_modules/.bin
It is this path that needs to be placed on the $PATH as described in the first comment.
Thanks guys.
npm installes executable to two places. By default running a npm install in a project will install any binaries in ./node_modules/.bin. When you use the -g flag (npm install -g package-name) it will install into a global path. You can find out the global path by running npm bin -g. Add that to your path and globally installed executables will be accessible.
You can also add ./node_modules/.bin to your path to allow easy access to executables added by packages in your project folder. I admit to using this on a trusted local machine. However, this is very dangerous and not a recommended way to expose the executables in the node_modules directory.
Best alternative is to add the executable to the scripts section of the package.json file and then use npm run-script <command> which will auto prepend the ./node_modules/.bin when executing.
package.json
{
"scripts": {
"foo": "foo --arguments"
}
}
Example
$ npm install foo
$ ls ./node_modules/.bin
foo
$ npm run-script foo
# Executes:
./node_modules/.bin/foo --arguments

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.

'Express' is not recognized command (windows)

Okay I am running node on windows (7). Using npm I just installed modules to d:\ directory. Therefore my files structure looks like the following:
D:\
-myproject
-node_modules
-.bin
-express
However, when I am in this 'myproject' directory, I can't seem to run 'express' for example:
D:\myproject\express site
'express' is not recognized as an internal or external command, operable program or batch file.
Am I doing anything wrong?
Try:
npm install -g express-generator#3
That solved problem for me.
Edit: for version 4
npm install express-generator -g
Description:
express is the package for dependency of express js.
express-generator is the package for enabeling express command and create a sample project, etc.
Assuming that this is kept separate for the decoupling of project dependency with cli tool of express.
Another SO ref: https://stackoverflow.com/a/41311733/1666582
Here's what to type in the command line to make it work in windows:
npm install express-generator -g
[Source: http://expressjs.com/starter/generator.html]
My guess is that you didn't install Express globally. You can install express globally (and therefore available in your PATH) with the following command (see http://expressjs.com/guide.html) :
npm install -g express
The way you install it is available only in the folder that you installed it and there is nothing wrong with that approach. There is very little advantage of having it available globally.
If express is not in your PATH you can run it by entering the full path to it:
\myproject\node_modules\.bin\express.cmd
With the release of Express 4.0.0 it looks like you need to do
npm install -g express-generator
We need to set path for express global directory
C:\Users[User_Name]\AppData\Roaming\npm\
After add a new path, please reopen the CMD console
Tried all of these and never worked. A repair of Node.js by kicking of installation and selecting repair option does the magic.
Cheers
What worked for me was:
I used the windows command prompt instead of the node.js command prompt.
In windows 10 simply type in the windows search bar for "node"
You see a node.js desktop app and a node js command prompt.
Choose the "node js command prompt"
Type in the command prompt
npm install express-generator -g
Then navigate somewhere and type in:
express your-website-text-here -e
A directory with express files will be generated. Also now you won't see the express error.
I was able to fix with the following package install:
npm install express-generator -g
Thanks
When you install Node.js, the below path is added to the Windows OS %Path% variable, I'm presuming similar happens on other operating systems as well:
C:\Users\<your-windows-username>\AppData\Roaming\npm
In my case, because I use a work Windows laptop for an employer that severely restricts what employees can do on their machines (I.e. many actions require elevated admin privileges), Node.js was being prevented from adding the above path to the Windows %Path% environment variable, and much to my chagrin the Node.js installation was silent about it. Navigate to above folder and you will notice the express command lives there, see screenshot below.
How did I figure this out? I did a fresh installation of Node.js on a personal, home Windows machine that has no admin privilege restrictions like my work machine does, compared the before and after %Path% value, and noticed the addition of that path. My work machine was missing it.
I had no choice but to add the path manually to %Path%, and then express was recognized from within any path I ran it.
I was able to fix this with:
npm install express-generator -g
I tried all the above solution, no luck for me.
Open "Node.js" command prompt and tried as administrator. It is working fine for me. Don't try with windows command prompt.
I have the same problem and understand the solution, but i can´t undestand why, running npm install -g express, express.bat isn´t added automatically to Path.
Running with npm install -g express have the same result. Download packages and store in node_modules, but express.bat isn´t added to path.
Run the node command prompt as administrator and then install express globaly
npm install -g express
and then go to folder where you want to install express generator, open command prompt there and run this command
npm install express-generator, it will then fix the issue
I have tried out all above solutions, but its did't worked for me, finally I have re-installed the node.js with newer version and started to express install process again. Its worked for me.
npm install -g express
npm install -g express-generator
What command are you using to open the directory?
That error means CMD can't find the "express" executable in the current directory.
Use the "PUSHD" command or "CD /D" instead of "CD"
#Echo OFF
PUSHD "D:\myproject\express" || (Echo bad folder)
express.exe "site"
Pause&Exit
Express is loaded someplace else and not in the windows path environment variable. You need to find were express.exe is installed and add the path. Maybe something like ;"C:\Program Files\Express\bin";
Running "npm install express" and "npm install express-generator" from your project directory will resolve the issue (if that helps).
But, this doesn't solve the problem of being global.
You might check the permissions to the folder if you are getting this when creating your project
Express Project
change script section in package.json file like this
"scripts": {
"start": "node app.js"
}
I too faced the similar problem and at last I tried using node.js command prompt instead of windows command prompt and it worked. So, try from node.js command prompt.
Tried all of these but didn't work for me. Also, I tried from different sources but never worked for me.
In the end, found that I need to run the command forcefully. It worked for me.
Make sure you run the command with Run as Administrator.
npm install -g --force express-generator
It will overwrite the existing express files.
Use npm start .. then the app.js runs .. which can be listened on the usual port 3000

Resources