44

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?

pewpewlasers
  • 3,025
  • 4
  • 31
  • 58

22 Answers22

167

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

saurabh
  • 729
  • 1
  • 7
  • 16
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
  • 5
    What does it exactly do. Related with version? – Adarsh Kumar Jul 17 '14 at 05:02
  • 6
    executed **npm install [express-generator](https://www.npmjs.org/package/express-generator) -g** to get the express binary. Also added the following USER path statement to system environment variables. _%USERPROFILE%\AppData\Roaming\npm\node_modules\express-generator;%USERPROFILE%\AppData\Roaming\npm;%USERPROFILE%\AppData\Roaming\npm\node_modules_ – dynamiclynk Sep 23 '14 at 18:30
  • Worked for me as well. I believe this just grabs a different build of express. – Fillip Peyton Mar 04 '15 at 04:25
  • Heads up! this will install `express v3`, if you already have installed that and you want to install `express v4` which is the latest version untill today, remove it from `%USERPROFILE%\AppData\Roaming\npm\node_modules` and reinstall it from npm: `npm install -g express-generator@4` – Yar Sep 03 '15 at 17:04
  • 1
    works on win10. Didn't add the @3 in the end, and no need to edit path – mgear Jun 15 '16 at 02:26
  • The problem with this generator is that it doesn't add `app.listen` statement in the auto generated app.js file. src: http://stackoverflow.com/a/23638240/4688650 – Merhawi Fissehaye Nov 23 '16 at 17:08
45

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]

Danny Pule
  • 1,061
  • 10
  • 5
41

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
Hector Correa
  • 26,290
  • 8
  • 57
  • 73
  • Hey @Hector thanks for the comment. I read somewhere, however, that there are issues with global install and that it should be avoided if possible. Is this true? – pewpewlasers Nov 26 '12 at 14:28
  • 1
    I don't know of any issues per-se. I just like having it local so that I can migrate each project to a newer version when I feel like it. – Hector Correa Nov 26 '12 at 16:29
  • In my case it didn't create express.cmd in .bin but node_modules\express\index.js exists. What does express.cmd look like? – Corey Alix Nov 12 '14 at 16:11
  • http://stackoverflow.com/questions/23002448/express-command-not-found indicated using -gd when installing express-generator. That created express.cmd – Corey Alix Nov 12 '14 at 16:16
  • 1
    I think this answer is incorrect - the command `npm install -g express` does not (for me, Win7) create the express.cmd file at the location specified. The next answer `npm install -g express-generator` installs it however correctly in the npm path `AppData\Roaming\npm`. – Marc Feb 13 '16 at 18:40
20

With the release of Express 4.0.0 it looks like you need to do

 npm install -g express-generator
baruchiro
  • 5,088
  • 5
  • 44
  • 66
Maniac One
  • 589
  • 4
  • 23
19

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

Yosi PW
  • 191
  • 1
  • 5
3

Tried all of these and never worked. A repair of Node.js by kicking of installation and selecting repair option does the magic.

Cheers

3

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.

Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
3

I was able to fix with the following package install:

npm install express-generator -g

Thanks

Piyush Bansal
  • 1,635
  • 4
  • 16
  • 39
3

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.

enter image description here

Jose Quijada
  • 558
  • 6
  • 13
2

I was able to fix this with:

npm install express-generator -g
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Miguel Veces
  • 165
  • 1
  • 7
2

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.

Srinivasan.S
  • 3,065
  • 1
  • 24
  • 15
1

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.

danipenaperez
  • 583
  • 5
  • 12
1

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

Awais
  • 69
  • 1
  • 6
1

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
Jaison James
  • 4,414
  • 4
  • 42
  • 54
0

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
0

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";

gigatropolis
  • 229
  • 2
  • 7
  • Could you please elaborate on this? I mean when I install express using npm, it seems to install express in the same folder state of the command prompt. What I mean is, if I am in D:\myproject\ and I type 'npm install express', it creates a node_modules folder in the same 'myproject' directory and installs express in it. I am sorry if I am being dumb :(. – pewpewlasers Nov 26 '12 at 09:30
  • Installing node.js adds the C:\Users\\AppData\Roaming\npm\ directory to your PATH variable. If the global install is not working correctly then something must have accidentally removed it. Running a repair-install of node.js should fix this issue – Zain Rizvi Mar 30 '14 at 05:59
0

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.

Vas Vasanth
  • 139
  • 2
  • 9
0

You might check the permissions to the folder if you are getting this when creating your project

Express Project

ricky
  • 41
  • 4
0

change script section in package.json file like this

"scripts": {
"start": "node app.js"

}

Vajiheh Habibi
  • 379
  • 1
  • 4
  • 16
0

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.

0

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.

Vinit Modi
  • 11
  • 4
-5

Use npm start .. then the app.js runs .. which can be listened on the usual port 3000