javascript - Can't require a module while using Mocha with Babel compiler -
i have 2 files: test.js contains
var buffer = require('./buffer'); and buffer.js with
this.foo when run them mocha test.js, works fine, running mocha --compilers js:babel/register test.js generates following error
typeerror: cannot read property 'foo' of undefined @ object.<anonymous> (/path/buffer.js:1:1) @ module._compile (module.js:460:26) @ normalloader (/path/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:150:5) @ object.require.extensions.(anonymous function) [as .js] (/path/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:163:7) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ module.require (module.js:365:17) @ require (module.js:384:17) @ object.<anonymous> (/path/parser_test.js:2:14) @ module._compile (module.js:460:26) @ normalloader (/path/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:150:5) @ object.require.extensions.(anonymous function) [as .js] (/path/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:163:7) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ module.require (module.js:365:17) @ require (module.js:384:17) @ /path/node_modules/mocha/lib/mocha.js:192:27 @ array.foreach (native) @ mocha.loadfiles (/path/node_modules/mocha/lib/mocha.js:189:14) @ mocha.run (/path/node_modules/mocha/lib/mocha.js:422:31) @ object.<anonymous> (/path/node_modules/mocha/bin/_mocha:398:16) @ module._compile (module.js:460:26) @ object.module._extensions..js (module.js:478:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:501:10) @ startup (node.js:129:16) @ node.js:814:3 why happening this undefined when require module? please let me know if need additional information reproduce error.
i use babel 5.5.0 , mocha 2.2.5.
babel expects input given es6/es2015 module. in modules, top-level this undefined specification.
babel --blacklist strict script.js allow disable behavior, not according specification, isn't best idea. can use global instead access global object in node, alternative.
for more information, see documentation.
Comments
Post a Comment