1

I am trying to run simple express server in node. But when I install the dependencies it's giving me error that node "Cannot find express".

here is console logs

asus@DESKTOP-PL2TIDL MINGW64 /f/nitishWorkspace/nitishGitRepo/chatApp (master)
$ npm install -g express
express@4.14.0 C:\Users\asus\AppData\Roaming\npm\node_modules\express
├── array-flatten@1.1.1
├── escape-html@1.0.3
├── encodeurl@1.0.1
├── content-type@1.0.2
├── range-parser@1.2.0
├── fresh@0.3.0
├── path-to-regexp@0.1.7
├── cookie-signature@1.0.6
├── etag@1.7.0
├── content-disposition@0.5.1
├── vary@1.1.0
├── serve-static@1.11.1
├── methods@1.1.2
├── merge-descriptors@1.0.1
├── parseurl@1.3.1
├── cookie@0.3.1
├── utils-merge@1.0.0
├── depd@1.1.0
├── qs@6.2.0
├── on-finished@2.3.0 (ee-first@1.1.1)
├── finalhandler@0.5.0 (unpipe@1.0.0, statuses@1.3.0)
├── debug@2.2.0 (ms@0.7.1)
├── proxy-addr@1.1.2 (forwarded@0.1.0, ipaddr.js@1.1.1)
├── accepts@1.3.3 (negotiator@0.6.1, mime-types@2.1.11)
├── type-is@1.6.13 (media-typer@0.3.0, mime-types@2.1.11)
└── send@0.14.1 (destroy@1.0.4, statuses@1.3.0, ms@0.7.1, mime@1.3.4, http-errors@1.5.0)

asus@DESKTOP-PL2TIDL MINGW64 /f/nitishWorkspace/nitishGitRepo/chatApp (master)
$ node server.js
module.js:327
    throw err;
    ^

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (F:\nitishWorkspace\nitishGitRepo\chatApp\server.js:1:75)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)

I think the problem lies in these lines

express@4.14.0 C:\Users\asus\AppData\Roaming\npm\node_modules\express

and

asus@DESKTOP-PL2TIDL MINGW64 /f/nitishWorkspace/nitishGitRepo/chatApp (master)

As you can notice the path is different here. I think that the dependencies are getting install in different directory and node is not able to access that. How can I solve this problem ?

Neo
  • 366
  • 5
  • 15
  • 1
    Maybe install express in your app directory. Global modules are used only if you want to run them from command line. So just run npm i express and you will be good to go. – Mykola Borysyuk Aug 12 '16 at 13:45

4 Answers4

1

Try installing express into your dependencies inside your package.json

npm install --save express

If you do not have a package.json you can create one using the following command from within the root of your project.

npm init -y

This command creates a package.json in your root directory using all the default values.

note

If you use:

npm init

You will be asked a series of questions, but you can set those up later by editing your package.json

See the docs here for more information about package.json

For more information about setting up dependencies see this answer on SO

edit addressing comment

Regarding using globally installed dependencies see this SO q&a

Community
  • 1
  • 1
alex
  • 5,467
  • 4
  • 33
  • 43
  • this again creates a local copy of dependencies in node_modules folder, I really don't want to do that. I just want to install them globally. – Neo Aug 12 '16 at 14:08
0

You should install express locally.. do npm install express in your chat app...

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
0

You probably want to install the ExpressJS CLI generator, if this is the case then run this command

npm install express-generator -g

And then you can type

express my_app

To create a folder with the whole structure inside. Then go inside the created folder:

cd my_app

and type: nam install, to install all the dependencies and express itself.

David Gatti
  • 3,576
  • 3
  • 33
  • 64
0

So I found the problem: The problem is with the PATH. The node is not able to find this path express@4.14.0 C:\Users\asus\AppData\Roaming\npm\node_modules\express so you can check out this --> answer.

I am not merging the questions because the issue here is different but the solution is same.

Hope this solve the problem.

Community
  • 1
  • 1
Neo
  • 366
  • 5
  • 15