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

javascript - Node.js cannot find an installed module - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Aug 2, 2015 at 21:10 Arcadio GarciaArcadio Garcia 5491 gold badge4 silver badges17 bronze badges 6
  • Maybe it have to be installed locally? – Rokie Commented Aug 2, 2015 at 21:15
  • @RoKie83 npm ls on that folder returns a list that does not contain it, so I think that is not the case – Arcadio Garcia Commented Aug 2, 2015 at 21:17
  • Please show us the code where you require() in xmlhttprequest. – jfriend00 Commented Aug 2, 2015 at 22:13
  • @jfriend00 It is not my code, as you can see in the stack trace is the socket-io module the one who requires it – Arcadio Garcia Commented Aug 3, 2015 at 9:18
  • Hmmm, something seems off here. I wonder why you need the xmlhttprequest module on the server. I've never had to install that module on my server when using socket.io. – jfriend00 Commented Aug 3, 2015 at 13:45
 |  Show 1 more comment

2 Answers 2

Reset to default 19

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.

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.

发布评论

评论列表(0)

  1. 暂无评论