简体   繁体   中英

Nodejs expressjs make npm package available throughout the project

I've this folder structure.

/app.js
/src
 /routes
 /controllers

In routes folder, I've bunch of js files. All of these files need to require passport js package like this

const passport = require('passport');

Instead of doing this, can I require the package in one place (Most probably in app.js) and somehow pass that to each and every file in the routes folder instead of requiring it on every file.

There may be a passport/Express-specific solution (eg, installing passport once as middleware), but answering the question about modules in general:

Requiring a module in a module that uses it is standard practice and clearly expresses the dependencies between modules, so it's not usually something you want to avoid doing.

Instead of doing this, can I require the package in one place (Most probably in app.js) and somehow pass that to each and every file in the routes folder instead of requiring it on every file.

You have a couple of options:

  • If all of those files have other things they're also all importing, you can create a rollup module that requires all of those things and then makes them available as exports. Then your files would do:

     const {passport, anotherThing, yetAnotherThing} = require("./the-rollup-module"); 

    instead of

     const passport = require("passport"); const anotherThing = require("another-thing"); const yetAnotherThing = require("yet-another-thing"); 

    The rollup would look like this:

     module.exports.passport = require("passport"); module.exports.anotherThing = require("another-thing"); module.exports.yetAnotherThing = require("yet-another-thing"); 
  • (I don't recommend this.) You can make it a global by putting this in your entry script:

     global.passport = require("passport"); 

    That exposes passport as a global variable, so your modules could just use passport without require ing it. (The default global variable is a reference to the global object, like window on browsers, so any property you create on it becomes a global variable.)

    I don't recommend it because then the dependencies between your modules are no longer clearly defined.

No matter how often you require() a module, it will be loaded just once. There is nothing wrong with requiring one module in multiple files, actually this is basically the way the module system is designed to work.

If the files in the routes folder are not using the express router you can rather or else export the function which accepts passport and app objects like

module.exports = function(app, passport) {
  app.get('/', (req, res) => {
      res.json('some route');
  });
}

Then you will be requiring the passport only once in the app.js/server.js and passing the same object to every route

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