简体   繁体   English

如何从单个节点项目发布多个NPM模块?

[英]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? 问题很简单:是否有某种方法可以从单个nodejs项目向npmjs注册表发布两个不同的NPM模块?

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. 而且我的nodejs应用程序整齐地分布在客户端,公共文件夹和服务器文件夹中。

  1. I often need to reuse the models in my common folder across other projects as well. 我经常还需要在其他项目中重用common文件夹中的模型。 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 而且虽然我可以使用.npmignore删除其他内容,但仅将该文件夹作为单独的NPM模块发布: /* /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. 我无法从当前发布的package.json删除混乱的东西,而这实际上是运行我的应用程序所必需的。 $ 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. $ npm pack $ tree -L 2 ~/dev/w2/node_modules/warehouse ~/dev/w2/node_modules/warehouse ├── README.md ├── common │ └── models ├── node_modules │ ├── ... │ └── ... └── package.json目前情况而言,我过滤掉了我的common/models文件夹以外的所有内容,但是由于我不确定在打包或打包之前如何动态调整package.json文件,作为打包的一部分... npm install将放下真正不需要的依赖项。

  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. npm link等是解决该问题的好方法,但是在加入新人员并确保没有人检查错误的情况下,这增加了开发人员设置的复杂性。

So is there some way to publish two different NPM modules to the npmjs registry from a single nodejs project? 那么,有什么方法可以从单个nodejs项目向npmjs注册表发布两个不同的NPM模块? Or some way to use different package.json file for a separate publication but all from one project? 或者以某种方式将不同的package.json文件用于单独的发布,但全部来自一个项目?


Update: What is my goal? 更新:我的目标是什么?

To reuse the models from my core project, across other projects, by publishing them as a NPM module. 通过将其发布为NPM模块来重用我核心项目中的模型以及其他项目中的模型。 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. 不放弃继续将我的核心项目开发为一个没有npm link等的原子实体的便利,因为当一个以上的人正在处理时,它会很快变得混乱。

Having a similar question, I recently discovered: https://github.com/lerna/lerna , which may be of use to you. 有一个类似的问题,我最近发现: https : //github.com/lerna/lerna ,这可能对您有用。

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). 为了解决这些(以及许多其他)问题,某些项目会将其代码库组织到多包存储库中(有时称为monorepos)。 Projects like Babel, React, Angular, Ember, Meteor, Jest, and many others develop all of their packages within a single repository. Babel,React,Angular,Ember,Meteor,Jest等项目以及许多其他项目都在一个存储库中开发了所有软件包。

Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm. Lerna是一种工具,可以优化使用git和npm管理多包存储库的工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM