简体   繁体   中英

Call a constructor function from an external .js file in node

I have actually an html file that loads a couple of js files:

<script type="text/javascript" src="jop.js"></script>
<script type="text/javascript" src="script.js"></script>

My objective is to migrate both scripts to node.js to run them standalone (without browser).

About the js files:

-jop.js: Compiled using the emscripten tool, contains a constructor function.

-script.js: Is the program itself

jop.js file content (beggining lines):

var PROPModule = function(Module) {
  Module = Module || {};

var Module;if(!Module)Module=(typeof PROPModule!=="undefined"?PROPModule:null)||{};var moduleOverrides={};// file continues....

script.js file content (original beggining lines):

var PROP = {};                          // PROP global object 
PROP['preRun'] = prerun;                // Will be called before PROP runs, but after the Emscripten runtime has initialized
PROP['onRuntimeInitialized'] = main;    // Called when the Emscripten runtime has initialized
PROP['TOTAL_MEMORY'] = 64*1024*1024;    // PROP Heap defaults 
PROPModule(PROP);                       // Calling the constructor function with our object

I´ve trying to call the constructor function "PROPModule(PROP); " in script.js using 'require' but none of the approachs works. This way:

script.js (modified in node. Beggining lines):

var jopjs = require('./jop.js');
var PROP = {};
PROP['preRun'] = prerun;  
PROP['onRuntimeInitialized'] = main;
PROP['TOTAL_MEMORY'] = 64*1024*1024;
jopjs.PROPModule(PROP);    

ReferenceError: jopjs is not defined

I´m quite new with js and node, and I´ve been searching for several days a solution without success.

Any suggestion please or idea how to call this constructor?.

.js file is read differently in browser and in nodejs. Hence, most probably your.js (coded for browser) won't be read in nodejs environment, vice versa.

What I did to make my js file compatible to both is wrapping my js code with this script:

/** jop.js */
; (function (window, factory) {
    if (typeof exports === 'object') {
        module.exports = factory();     // NodeJs Environment
    } else {
        window.jop = factory();         // Browser Environment
    }
}(this, function () {
    return (function (window) {
        "use string";

        let PROPModule = function (Module) {
            console.log('PROPModule is called');
        }

        // exposed functions
        return { myFunc };
    }(this));

}));

While this is how you can import it in NodeJS environment:

script.js

const jopjs = require('./jop')

While this is how you can import it in browser environment (either inside or ):

<script type="text/javascript" src="./app/lokidb-seed.js"></script>
<script>
  console.log('The "jop" word in window.jop = factory();: ', jop.PROPModule());
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM