如何使用 NPM 处理 git 项目中的 Node.js 依赖项?

发布于 2024-12-07 09:00:27 字数 554 浏览 0 评论 0原文

我已经多次遇到这种情况,但仍然没有找到答案。我正在启动一个新的 Node.js 项目,该项目将依赖于其他一些库。为了方便讨论,我们假设有些是纯粹的 JS 库,可以作为 git 子模块添加到新项目中,但有些则需要一些额外的工作(例如 npm 安装的系统依赖项,或必须编译的 C 库) )。

启动这个项目并将其添加到 git 的最佳方式是什么,需要满足以下两个要求:

  • 其他人的库没有提交到我们自己的存储库,而是子模块或动态拉入并由 npm 安装。
  • 不需要有一大堆必须遵循的指令列表来克隆存储库并拥有一个工作环境。运行 git submodules update --init --recursive 可以,运行 npm 命令来读取 package.json 并安装依赖项也可以(这样的命令存在吗?),但强制每个人都运行“npm install 每个依赖项的 __" 都是不行的,如果不需要的话,我宁愿不使用 'make' 或 'ant' 来做到这一点。

有什么最好的方法来做到这一点的想法吗?这看起来是一件简单、基本的事情,但我找不到我想要做的事情的一个例子。

编辑:语法

I've run into this scenario quite a few times and still haven't found the answer. I'm starting a new Node.js project, and this project will be dependent on some other libraries. For sake of argument, let's say that some are purely JS libraries that could be added as git submodules in the new project, but some have pieces that need some extra effort (such as system dependencies that npm installs, or C libraries that must be compiled).

What's the best way to start this project and add it to git, with the following two requirements:

  • Other people's libraries aren't committed to our own repo, and are instead submodules or are pulled in dynamically and installed by npm.
  • Not needing to have a big list of instructions that have to be followed just to clone the repo and have a working environment. Running git submodules update --init --recursive is fine, running an npm command to read package.json and install the dependencies is fine (does such a command exist?), but forcing everyone to run through an "npm install __" of every single dependency isn't ok, and I'd rather not use 'make' or 'ant' to do that if I don't have to.

Any thoughts of the best way to do this? It seems like such a simple, basic thing, but I couldn't find a single example of what I'm trying to do.

Edit: Grammar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-12-14 09:00:27

编辑 忽略下面,但留下参考。有时我早上并没有想清楚:)

创建一个package.json文件,添加你的依赖项,你的安装就变成了:

npm install

从你的项目目录。 git 忽略所有添加的项目。


npm submodule foo

它通过 git submodule 将包安装到 node_modules 中,这样 github 等就会识别出它们是链接的。只要 npm 包包含 git URI,这就会起作用。不幸的是,很多人都没有这样做,所以你在这些方面运气不好。

另请注意,当您执行此操作时,npm 将不再在该模块上工作,例如您无法通过 npm 进行更新,您必须通过 < code>git


或者你可以这样做:

./modules.js

modules.exports = [ '[email protected]', '[email protected]', '[email protected]' ];

./make

#!/usr/bin/env node
var modules = require( './modules' )
  ,   spawn = require('child_process').spawn;

for( var i=0, l=modules.length; i<l; i++ ){
    spawn( 'npm', [ 'install', modules[i] ] );
}

edit Ignore below, but left for reference. Sometimes I don't think clearly in the morning :)

make a package.json file, add your dependencies and your install simply becomes:

npm install

from your project directory. git ignore all the added projects.


npm submodule foo

It installs the packages into node_modules via git submodule so github, etc will recognize that they're link. This works whenever the npm package has a git URI included. Unfortunately a good number don't, so you're out of luck on those.

Also, note that when you do this, npm won't work on the module any longer, e.g. you can't update via npm, you have to do it via git


Or you could just do something like:

./modules.js

modules.exports = [ '[email protected]', '[email protected]', '[email protected]' ];

./make

#!/usr/bin/env node
var modules = require( './modules' )
  ,   spawn = require('child_process').spawn;

for( var i=0, l=modules.length; i<l; i++ ){
    spawn( 'npm', [ 'install', modules[i] ] );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文