简体   繁体   中英

Reusing my own JavaScript modules without using relative paths

I am new to JavaScript development and could do with some advice regarding how best to work with multiple modules of my own creation.

Module1 --src ----a.js ----b.js

Module2 --src ----c.js ----d.js

Module3 --src ----e.js ----f.js

Module4 --src ----g.js ----h.js

Now each module is a collection of js files that use module.exports to export functions that they need to.

In this example Module1 and Module2 are library module. Module2 depends on Module1. Both of these modules are platform agnostic and can be ran inside a browser or nodejs.

Module3 is a project module which depends on both Module2 and Module1. It is designed for a browser and uses browserify to combine them all into a bundle.

Module4 is another project module which also depends on Module2 and Module1. This is designed to run under nodejs.

Each module has its own git repo.

Now the problem I am having is to do with relative paths inside the require. For example c.js currently does require("../../Module1/src/a.js"); Similarly h.js does require("../../Module2/src/c.js");

This causes foldering structure to be an absolute pain and each project has to be cloned from git in the correct setup.

The following is what I am trying to achieve.

  1. To do away with relative paths so I simply do require ("ModuleX/src/foo.js"); This needs to work with both browserify and nodejs.

  2. To clone a project module from git and have it get all of its dependent modules (perhaps git submodules?). Not caring about folder structure as this should be solved using the point mentioned above.

  3. Once a project and it's dependent modules have been cloned to be able to edit each of these modules, make changes and push them to their individual git repo.

I imagine what I am trying to do is pretty standard. Creating my own modules and reusing them in different projects. Am I trying to go about solving it in the standard way? I've read that there are different ways to achieve this using NODE_PATH which is supported by both node and browserify but both discourage it. Browserify supports a paths option but doesn't work for node. Also read about putting the modules inside node_modules but not sure how that would help with relative paths.

Any advice would be greatly appreciated

Thanks

What you probably want to do is commit your reusable code to git (here GitHub) and then npm install <git remote url>: . An example would be npm install git+https://isaacs@github.com/npm/npm.git . On the other hand your repo needs a package.json and a index.js holding your code or pointing to it. Alternatively you could define the location of your main file in the package.json via main:"" key.

The dependencies between those projects would be defined in the respective package.json s. Upon install npm does it's magic to avoid circular dependencies, caching etc.

Once you've done all that you would be able to just var x = require("ModuleX") to get ModuleX/src/foo.js if you so wish to.

Alternatively use npm s private modules.

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