简体   繁体   中英

Can't find module in nodejs

I am facing this issue. when i am trying to run app.js

Error: Cannot find module './protocol/json'
        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:\xampp\htdocs\project\dev4\nodejs\node_modules\a
    ws-sdk\lib\core.js:31:11)
        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)

Any idea.?

Thanks

If you are using node-webkit current directory would in fact be where index.html is located not in the current directory of the module in question. If you are not using node-webkit this is simply a failure to point to the correct location of the file.

EDIT: If I am not mistaken, current directory may not be what you think depending on how the node.js script is executed and from where, but don't hold me on that please ;)

In any case, and to avoid possible caveats with file paths I advise you to use path module that comes with node.js

Simply add this code inside a module you are requiring protocol/json from. (be mindful that if protocol directory is not the the current directory it will still fail to require it).

var path = require('path');
var json = require( path.resolve(__dirname, './protocol/json') ); 

I was seeing this because I had forgotten to npm install aws-sdk in a project. I had somehow gotten a node_modules directory in my home directory, which had an old version of aws-sdk that was being used instead.

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