最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't node.js load the module I require? - Stack Overflow

programmeradmin1浏览0评论

I have node.js install on linux, and file.js. In the same directory I have node_modules directory with lru-cache module.

file.js does the following:

var lrucache = require('lru-cache')

But when I run it, it raises the following error:

Error: Cannot find module 'lru-cache'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/opt/file.js:58:12)
    at Module._pile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

What is the problem? the same is working in other linux system.

I have node.js install on linux, and file.js. In the same directory I have node_modules directory with lru-cache module.

file.js does the following:

var lrucache = require('lru-cache')

But when I run it, it raises the following error:

Error: Cannot find module 'lru-cache'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/opt/file.js:58:12)
    at Module._pile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

What is the problem? the same is working in other linux system.

Share Improve this question edited Aug 3, 2014 at 9:52 m90 11.8k15 gold badges67 silver badges119 bronze badges asked Aug 3, 2014 at 9:01 Or SmithOr Smith 3,62613 gold badges47 silver badges70 bronze badges 5
  • 1 Can you post a screenshot of your directory structure? There's probably sth wrong here. Btw I guess it's a rather bad idea to use fs as a variable name for lru-cache as you'll probably want to require the fs module at some point. – m90 Commented Aug 3, 2014 at 9:04
  • @m90: I correct the variable name. Here is the tree relevant struct: ` . |-node_modules |---lru-cache |-----lib |-----test ` – Or Smith Commented Aug 3, 2014 at 9:09
  • Did you install the package using npm install? – m90 Commented Aug 3, 2014 at 9:10
  • @m90: No, I just move the directory. But this method works in other linux systems. – Or Smith Commented Aug 3, 2014 at 9:11
  • 1 Then you are probably missing the module's own dependencies. ALWAYS install packages using npm. Just locate your root, do npm init, answer the questions, then do npm install lru-cache --save and you're ready to go. – m90 Commented Aug 3, 2014 at 9:12
Add a ment  | 

2 Answers 2

Reset to default 2

Please try:

rm -rf node_modules && npm cache clean && npm install

Sometimes npm has an issue and the dependency lru-cache may not get properly installed.

Most node modules will have their own set of package dependencies so you cannot just copy the folder or clone the repository without making sure you are satisfying the module's dependencies.

The easiest way would be using npm for ALL package installations.

After you have run npm init in your project's root directory to set up your package.json use

$ npm install modulename --save

to install a package AND its dependencies. You can now safely use

var module = require('modulename');

throughout your whole project.

In case you cannot install your package via npm make sure all of its dependencies are installed as well by navigating to node_modules/modulename and running npm install (no arguments) here. This will install all dependencies that are listed in the modules own package.json file.

发布评论

评论列表(0)

  1. 暂无评论