简体   繁体   中英

How should I require modules effectively from outside of node_modules folder?

I have a bunch of modules in my application, what I require relatively from the current script. The problem with this if I want to restructure my application then I have to change tons of require statements.

So I started to use something like this:

//loader.js in root

"use strict";

exports.Logger = require('./core/logger');
exports.Module = require('./core/module');

And in my files I use require it like this:

"use strict";

var Loader = require('../../loader');
var Logger = Loader.Logger;
var logger = new Logger();

So now if I want to restructure the shared codes in my application then I have to change the require only in the loader.js file. Is this a good solution? What do you use?

Is there any way to make require fully automatic when the required module name is unique?

Why are your modules not stored in directories? For example:

/ app.js lib --/logger ----/index.js

then in app.js you can just require(./lib/logger)

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