can not find module express --on windows

2019-02-20 04:58发布

问题:

I use "npm install -g express" on windows console.but when I try to "node app.js", it shows me the error"can not find module express",I had set the environment variable"NODE_PATH",but nothing happen ,I need your help,Thank you!

回答1:

Globally installed modules aren't accessible without full path. You need to install express in your project directory or it parents. Check out documentation about module loading.



回答2:

npm allows two options on how to install a module: locally and globally.

A global installation (done using npm install -g xyz) is for providing some tooling system-wide. Related to express this provides the global express bootstrapper that you can use to create an initial frame for your app by simply typing: express .. If you need help on what you can do with this command, check out its help parameter: express --help.

In contrast, a local installation of a module provides this module for a specific app. A local installation is always made to an app's node_modules folder. When you try to require a module, Node.js searches the this folder for the requested module.

Hence, it is perfectly fine to have express installed multiple times: Once globally for the bootstrapper, multiple times locally (once per app).

So, to cut a long story short: To make your app run, install express locally using npm install express and that's it :-).