Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 458
pulkitsinghal Avatar asked Jun 20 '15 17:06

pulkitsinghal


People also ask

How install multiple modules npm?

To install multiple packages, we need to use the npm install followed by the multiple package names separated by the spaces package1 package2 . This above command installs three packages, which are express, cors and body-parser. You can also checkout how to install the specific version of an npm package.

CAN node modules be shared?

Option 1: Link to a Local Project Folder Once you've moved your shared code into a separate project, link the project as a dependency using npm link. Note: The shared library can be maintained in a separate repository or the same repository as your other projects (a.k.a, monorepo).


1 Answers

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.

like image 140
cmroanirgo Avatar answered Oct 20 '22 00:10

cmroanirgo