16

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.

Michael.Lumley
  • 2,158
  • 2
  • 27
  • 49
pulkitsinghal
  • 3,273
  • 8
  • 40
  • 72
  • 1
    "And although I can like to publish just that folder as a separate NPM module" > great "...by using .npmignore to remove the other stuff ..." ...huh? Can't you separate it *out* from the app, and use it as a module in your app like require('common')? Maybe you could post some minimal code like package.json and your file structure to give a better idea of your situation. – laggingreflex Jun 20 '15 at 18:00
  • @laggingreflex - updated the language ... as for `separate it out from the app` ... as I point out in bullet 2, I'm asking this question so I don't have to do that, I'm looking to push the envelope here a bit. – pulkitsinghal Jun 20 '15 at 18:11
  • I wish folks would help out by providing a reason for downvoting other than just leaving me to guess. – pulkitsinghal Jun 20 '15 at 18:42
  • You have created an [X-Y problem](http://meta.stackexchange.com/q/66377/202302). Your original goal to separate out a common piece of code and use in two separate projects is great! But the solution you've come up with to achieve it, and in which you're now having problem, is a misguided one and may do more harm than good. Instead of asking "how to publish multiple NPM modules...", you should just ask a question that helps you achieve your original goal, providing more details, like what exactly is inside your `common` and how it interacts with your project.. – laggingreflex Jun 20 '15 at 19:17
  • Did I not meet this requirement: `don't try to avoid going over them again – instead state why you've ruled them out` when I wrote: `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` – pulkitsinghal Jun 20 '15 at 19:53
  • I've removed loopbackjs as I don't wish to fight the community but to whomever suggested that edit, please note that this scenario, though common to any nodejs user ... would be surely relevant to loopbackjs users who are trying to modularize ... as models are baked into their framework and lend themselves to reuse naturally. But I imagine trying to target fellow loopbackjs users is wrong in your opinion when trying to get feedback on this topic that they might have already tackled? So yes I took your suggestion and removed the tag. – pulkitsinghal Jun 24 '15 at 15:24

1 Answers1

9

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.

cmroanirgo
  • 6,951
  • 4
  • 26
  • 36