javascript - Why can't I call any functions from a separate .js file? -


if have following main.js file:

require('./test.js');  $(document).ready(function(){     testfunction(); }); 

and cooresponding test.js file in same directory main.js:

function testfunction() {     console.log('from test.js'); } 

i error:

uncaught typeerror: testfunction not function

if try set require statement variable x, , call x.testfunction in main js file, same error x.testfunction.

how work? need able call functions separate js files.

you need export file have function in:

function foobar() {     console.log('hi'); }  module.exports = foobar; 

then, can use in other file like:

var foo = require('./foobar'); foo(); 

if want export multiple functions file, may use object, too:

module.exports = {     foobar: foobar,      baz: baz }; 

and use it:

foo.foobar(); foo.baz(); 

there many other options , possibilities, make sure read docs too.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -