0
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.

sampathsris
  • 21,564
  • 12
  • 71
  • 98
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

3

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.

milkandtang
  • 576
  • 3
  • 6
  • hmmm then how come express worked when I installed it globally, makes no sense? – Alexander Mills Sep 26 '14 at 05:46
  • I did get a warning that there was "no repository field" when I ran "npm install --save backbone"...my package.json file doesn't have that field defined..."repository": "" – Alexander Mills Sep 26 '14 at 06:00
  • npm will warn when there's no repository field in your package.json, but it will not cause any problems. You can [read here](https://www.npmjs.org/doc/files/package.json.html) about the package.json fields that are available, and what each expects. – milkandtang Sep 26 '14 at 18:17
  • 1
    as to why Express works; did you `npm link express` in your local folder? technically it should not work unless you did. Here is another [stackoverflow post](http://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package) on the subject. – milkandtang Sep 26 '14 at 18:20