Nodejs Cannot find module

浪子不回头ぞ 提交于 2019-11-30 06:14:48
Hien Khieu

You should install Express locally:

npm install express

Then require it as you did:

var express = require('express')

Just to quote from here:

https://www.npmjs.org/doc/files/npm-folders.html

  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.
  • If you need both, then install it in both places, or use npm link.

I was getting same error on Windows7/x64 and adding following in the environment variable resolved the issue:

NODE_PATH=C:\Users\[USERNAME]\AppData\Roaming\npm\node_modules

*Replace [USERNAME] with your actual system username

I'm working in Linux, but when I require express, I'm doing so with a relative path to where it is installed and it works fine:

var express = require('./public/node_modules/express');

I'm sure the same thing would work with a windows path as well. If you want to be more explicit and declare an absolute path, that would be the nuclear option to make sure you always know exactly where your module is being loaded from regardless of where your scripts are being run from.

If you still have a problem after using an explicit path, I don't know what the problem might be. . .

another option will be to run npm install --save express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!