简体   繁体   中英

How to publish multiple NPM modules from a single node project?

The question is pretty straightforward: Is there some way to publish two different NPM modules to the npmjs registry from a single nodejs project?

But for those of you wondering why the heck would someone even want to do that? ... the motivation behind this question may not be too obvious. So allow me to explain:

I use something akin to an app-server called loopback. And my nodejs app is neatly distributed into client, common and server folders.

  1. I often need to reuse the models in my common folder across other projects as well. And although I can publish just that folder as a separate NPM module by using .npmignore to remove the other stuff: /* /common/utils /common/models/*.js !/common/models/*.json

    I cannot remove the clutter from my current package.json , which is needed to actually run my app, from what has been published. $ npm pack $ tree -L 2 ~/dev/w2/node_modules/warehouse ~/dev/w2/node_modules/warehouse ├── README.md ├── common │ └── models ├── node_modules │ ├── ... │ └── ... └── package.json As things stand, I filtered out everything other than my common/models folder but since I'm not sure how to dynamically tweak the package.json file before packing it or as part of packing it ... an npm install will put down the dependencies that really aren't required.

  2. I am not thrilled at the alternative of creating a separate project solely with models in it as that forces me to add it as a dependency to the core project and active development gets split across 2 projects where it used to be just one. npm link etc is a good approach to get around it but it adds complexity to developer setup when onboarding new folks and making sure that nobody checks in the wrong thing.

So is there some way to publish two different NPM modules to the npmjs registry from a single nodejs project? Or some way to use different package.json file for a separate publication but all from one project?


Update: What is my goal?

To reuse the models from my core project, across other projects, by publishing them as a NPM module. WITHOUT giving up the convenience of continuing to develop my core project as ONE atomic entity without npm link etc. as that gets messy fast when more than just one person is working on it.

Having a similar question, I recently discovered: https://github.com/lerna/lerna , which may be of use to you.

From the readme:

Splitting up large codebases into separate independently versioned packages is extremely useful for code sharing. However, making changes across many repositories is messy and difficult to track, and testing across repositories gets complicated really fast.

To solve these (and many other) problems, some projects will organize their codebases into multi-package repositories (sometimes called monorepos). Projects like Babel, React, Angular, Ember, Meteor, Jest, and many others develop all of their packages within a single repository.

Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.

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