nodejs - can't find module Backbone - node.js

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.

Related

Gulp watch not working - cannot find module connect

Trying to run my project using gulp (havent used gulp before and have inherited an exisiting project).
I install the dependencies into the wordpress theme directory.
Running gulp watch throws this error:
Error: Cannot find module 'connect'
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:\dev\projects\romac-website\web\app\themes\romac\node_modules\browser-sync\lib\utils.js:307:31)
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)
I have tried to update npm but not sure if it is fine or the problem (still).
I also tried to ins install this module with npm -g install connect
What should I try next?
Try deleting the node-modules folder then reinstall npm install connect --save-dev Then retry running gulp

coreMIDI and NodeJS

I've installed coreMIDI following this:
sudo npm install -g coremidi
The installation was successful, but I don't understand how to use it using node.js
This is the first line of my code:
var coremidi = require('coremidi')();
But the output is:
Error: Cannot find module 'coremidi'
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> (/Users/[NAME]/Desktop/nodeMidi/coremidi/test1.js:1:78)
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)
The require statement in node looks for a folder named node_modules in the current folder and if that exists looks in there for a folder named after your package. If it isn't found, it goes one directory higher and tries that again. It'll keep doing this until it reached the root folder of your file system.
The reason it can't find coremidi is because coremidi has been installed in the global node_modules folder since you used the -g flag. This is OS/node install dependent, but usually something like /usr/lib/local/share/npm/ or whatever. Since this path
You shouldn't use -g to install npm modules unless they're specifically meant to be installed globally (usually that means that include a script for you to run, like NPM itself, or jshint, or mocha, or tap, or etc...)

node.js returns "Cannot find module 'lockit-mongodb-adapter'"

I am going through my first node.js project. I've installed lockit , have a app.js file, and when I try to run it I get this error
module.js:340
throw err;
^ Error: Cannot find module 'lockit-mongodb-adapter'
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 new module.exports (C:\Users\xx\Documents\mailer\index.js:38:44)
at Object.<anonymous> (C:\Users\xx\Documents\mailer\examples\mongodb\app. js:32:14)
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)
I am quite certain I have lockit-mongodb-adapter installed.
You need to install the module. Cd to the root of your app directory and run this command and make sure lockit-mongodb-adapter is in your node modules directory.
npm install lockit-mongodb-adapter
Another thing you can do if you are certain it is installed is double check to see what is installed by running this command:
npm list
And this one to see what is installed globally:
npm list -g
Update:
I was able to reproduce the issue you where having. And to resolve it I deleted the module from the node moules directory. And re-installed with the following command:
npm install lockit-mongodb-adapter -save
It now shows up in npm list with no issues:

Node js Express module.js:340

I'm having trouble running my app in node. When I first got the module.js:340 error, I used a sudo npm install express -g, and it looked like Express is installed but when I run my app again I get this error.
module.js:340
throw err;
^
Error: Cannot find module 'express'
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> (/Users/mac/Downloads/node-FED-master/app.js:6:15)
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)
users-Mac-Pro:node-FED-master mac$
You've installed Express globally (with -g), which gives you access to the express command line tool, but Node programs can't access the global module. You need to install Express locally by doing npm install express in your project directory.
If the project has a package.json, a simple npm install in the project directory will install all necessary dependencies in it's correct versions.

Express js installation error in node

While trying to install express js by the command "npm install -g express" in cmd prompt in windows it shows the following error
module.js:340
throw err;
^
Error: Cannot find module 'express'
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> (D:\chapter3\sample-app\web-server.js:1:77)
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 to fix this and run a web server?
You shouldn't install express globally. cd into your project directory, then npm install express.
Global package install is meant to install command line utilities; project dependencies should be installed locally, see npm's FAQ and this blog post.

Resources