javascript - Node.js 'require'ing other files without needing namespaces -


i switching codebase 100% browser side, mixture of browser side , server side.

the issue have found code run using node.js must use modules. in order make code modules, require huge refactor of of code. reason on browser many functions used across files without worrying namespacing.

but currently, in node.js have found no way achieve same affect, , solutions have found have not worked.

for example of like, able this:

//////////// // file1.js ////////////  function somefunction(someargs) {     /* run stuff, calculate stuff */      return something; }  //////////// // file2.js ////////////  function somefunction2(someargs) {     /* run code */      let somevalue = somefunction(someargs);      /* run more code */ }  //////////// // file3.js ////////////  somefunction2(myarguments); 


have tried following solutions found here, have not helped me.


"masylum"'s answer not need.


"udo g"'s answer, referring using eval include other file in running file throws error:

console.log(iswhitelisted("test"));             ^  referenceerror: iswhitelisted not defined     @ object.<anonymous> (/users/< snip >/server.js:45:13)     @ module._compile (module.js:413:34)     @ object.module._extensions..js (module.js:422:10)     @ module.load (module.js:357:32)     @ function.module._load (module.js:314:12)     @ function.module.runmain (module.js:447:10)     @ startup (node.js:139:18)     @ node.js:999:3 

this although pointed out udo g using strict mode:

please note won't work "use strict";


"nick panov"'s answer did not work me, throwing same error udo g's answer. might because of use strict.


is there way can achieve without refactoring whole codebase adhere using modules?

try use building step achieve this. simple concat of files you. example, @ https://github.com/contra/gulp-concat

here docs related gulp - https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -