简体   繁体   中英

nodejs - can't find module Backbone

var Backbone = require('backbone');

causes this error:

module.js:340
    throw err;
          ^
Error: Cannot find module 'backbone'
    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> (C:\Users\denman\workspace-nodejs\AFirstServer_NodeUpAndRunning\hello-world-server.js:6:16)
    at Module._compile (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)

how can this be?

I installed Backbone via npm install -g backbone

I even restarted Eclipse and my machine.

By default, node will not load modules that are installed globally. You should be performing any npm install -s in your project's directory root, not installing them globally.

Try this instead. Create your new project directory, change to it, and then:

npm init  #this will create a package.json for your project
npm install --save backbone  # this will install backbone to the directory, and save it into your package.json as a dependency

Then create a file in that directory called index.js . Put your code in there requiring backbone. Then from within that directory run node index.js , and you'll find that everything works, and backbone is available.

Here is a good blog post on the subject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM