javascript - include a module into another nodeJS file -


i getting started node , wrote little s3 reader looks following. sits in projectroot/routes/s3store.js directory.

var aws = require('aws-sdk'), uploader = require('s3-upload-stream')(new aws.s3()), q = require('q');  module.exports = s3store;  function s3store(config) {     this.config = config;     this.s3 = new aws.s3({     region: config.aws.region     }); }  s3store.prototype.readstream = function (filepath, metadata) { var readconfig = {     'bucket': this.config.s3.bucketname,     'key': filepath     }; return this.s3.getobject(readconfig).createreadstream(); } 

now, created new file s3storemain.js in projectroot/routes/s3storemain.js , trying import s3store.js module this:

var s3 = require('s3store'); 

on running:

node s3storemain.js 

it cannot find module s3store. here stacktrace:

shubham@turing:~/downloads/node-v0.12.4/exp2014$ node routes/s3storemain.js   module.js:340     throw err;       ^ error: cannot find module 's3store'     @ function.module._resolvefilename (module.js:338:15)     @ function.module._load (module.js:280:25)     @ module.require (module.js:364:17)     @ require (module.js:380:17)     @ object.<anonymous> (/home/shubham/downloads/node-v0.12.4/exp2014/routes/s3storemain.js:1:72)     @ module._compile (module.js:456:26)     @ object.module._extensions..js (module.js:474:10)     @ module.load (module.js:356:32)     @ function.module._load (module.js:312:12)     @ function.module.runmain (module.js:497:10) shubham@turing:~/downloads/node-v0.12.4/exp2014$  

what missing?

read nodejs api reference

in order require modules files relatively need use relative paths... how require module designed with-in nodejs (read api docs linked more information)

try requiring s3store file s3storemain file so:

var s3 = require("./s3store"); 

you can omit ".js", it's optional.


note saying need put modules.exports @ end of file... false!! due how defined function, referencing shouldn't matter.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -