So I have some node.js files
/folder/app.js
/folder/node_modules/moduleIwanttoload
/folder/subfolder/file.js
how do I require moduleIwanttoload
from file.js?
In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
Module. _load performs the loading of new modules and manages the cache. On a file loading request, it will first check the file in cache. If module is not found in the cache, this will create a new base module instance for the required file.
you can use subfolder files like this into app.js
var file = require('./subfolder/file.js');
and in the folder of node-modules you can just use it like
var moduleIwanttoload = require("moduleIwanttoload");
If the module you want to load is a dependency contained in "node_modules" you can simply use
require("moduleIwanttoload")
Node JS will trace the directory tree down to the first folder in which it finds a "node_modules" directory and looks for the given dependency in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With