node.js - require difference in javascript(spidermonkey) and nodejs -


in javascript(spidermonkey)

//t1.js

var a=function(){   };  a.prototype.dosomething=function(){     console("t1");  } 

t.js

require ('t1.js'); var b1=new a(); b1.dosomething(); 

it equals:

var a=function(){   };  a.prototype.dosomething=function(){     console("t1");  } var b1=new a(); b1.dosomething(); 

but in node.js

t.js

require ('./t1.js'); var b1=new a(); b1.dosomething(); 

it reports error:

node t.js

t.js:  var b1=new a();            ^ referenceerror: not defined     @ object.<anonymous> (/users/mymac/js/t.js:16:12)     @ 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)     @ startup (node.js:119:16)     @ node.js:906:3 

your comment welcome

nodejs's module system not leak globals (unless asked to).

if need export reference it, use module.exports such:

//t.js module.exports = a;  // requiring file var = require ('./t1.js'); var b1=new a(); b1.dosomething(); 

keeping our global environment clean. in es6 times, done module loaders.

see this official documentation piece on modules learn more.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -