Arcadio Garcia
Arcadio Garcia

Reputation: 549

Node.js cannot find an installed module

After deploying my node.js app to another PC (in the dev machine worked perfectly) and installing all the dependencies manually, I get this error when I try to execute it:

    C:\Users\myself>node app.js
module.js:340
    throw err;
    ^

Error: Cannot find module 'xmlhttprequest'
    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:\Keystroke\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\lib\transports\index.js:5:22)
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (c:\Keystroke\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\lib\socket.js:5:18)

But if I run

npm ls -g

It returns the list of the globally installed modules, and it includes xmlhttprequest. Then why my app is unable to find it? What am I doing wrong?

Upvotes: 4

Views: 17262

Answers (2)

Sean
Sean

Reputation: 3042

what do you mean by

installing all the dependencies manually?

usually, we will install the dependencies by npm install --save or npm install --save-dev and when I need to migrate to another enviroment, I just need to clone the source code and fire npm install from the root of the project, and it will do all the magic for me.

sometimes it will have problem if the package you are use is a c++ addon, it will failed to install that if the node version you are using is different with your dev enviroment and prod envirment, be careful. I usually use nvm for my node version manangement, nvmw for windows, that can save me lots of time.

Upvotes: 1

eddyjs
eddyjs

Reputation: 1280

The module probably needs to be locally installed for the project as well.

Do you have a package.json file? If so, run:

npm install --save xmlhttprequest

in your repo directory next time so when your switch machines, you can run npm install to retrieve all of the dependencies.

Some dependencies are not useful when they are globally installed on the machine.

Upvotes: 19

Related Questions