简体   繁体   English

在git控制下在node_modules中获取npm模块

[英]getting npm modules in node_modules under git control

I have many git repositories each with their own npm package.json file that reference each other. 我有很多git存储库,每个存储库都有各自的npm package.json文件,它们相互引用。 When I install the main package with npm install it successfully clones all git repos referenced in all package.json into the node_modules folder. 当我使用npm install安装主软件包时,它将成功将所有package.json中引用的所有git repos克隆到node_modules文件夹中。 However they are not cloned with the .git folder for me to keep any changes under source control. 但是,对于我来说,它们没有与.git文件夹一起克隆,以使所有更改都受源代码控制。 Is it possible to npm install these packages and to get the .git folder? 是否可以npm安装这些软件包并获取.git文件夹?

Thanks 谢谢

They're not for everyone but I really like using git submodules for such things so I wanted to work out a way to use them for this: 它们并不适合所有人,但我真的很喜欢使用git子模块进行此类操作,因此我想找到一种方法来使用它们:

  1. Add submodule under local_modules/ : local_modules/下添加子模块:

     git submodule add -b develop git@github.com:JarvusInnovations/git-client.git local_modules/git-client 
  2. Use file: prefix to declare dependency in package.json : 使用file:前缀在package.json声明依赖项:

     { "dependencies": { "git-client": "file:local_modules/git-client" } } 
  3. Refresh node_modules/ : 刷新node_modules/

     npm install 

Now NPM creates a symlink under node_modules/ to ../local_modules/git-client and you can commit whatever HEAD you want your submodule to be at to the main project. 现在,NPM在node_modules/下创建到../local_modules/git-client的符号链接,您可以将希望子模块放在主项目中的任何HEAD提交。 So your dev workflow can look like 因此您的开发工作流程看起来像

  1. Make a set of changes across the main project and the module that go hand-in-hand 对主项目和模块进行并行的一组更改
  2. Make a commit within the submodule to add/change features 在子模块中进行提交以添加/更改功能
  3. Make a commit within the main project that includes both the change in submodule version and the changes in the main project that go along with it locked together 在主项目中进行提交,其中既包括子模块版本中的更改,也包括与之一起锁定的主项目中的更改

Now when other developers clone ( --recursive ) or pull (and then run git submodule update --init ) your pre-release work they can be guaranteed to have the right pre-release module code that goes with it 现在,当其他开发人员克隆( --recursive )或拉(然后运行git submodule update --init )进行预发布工作时,可以保证他们具有正确的预发布模块代码

In general files inside node_modules are expected to be managed by npm . 通常, node_modules内部的文件应该由npm管理。 Node wasn't originally designed that way but npm has evolved to become the standard package management tool for node.js (it even ships with node!). Node最初并不是以这种方式设计的,但是npm已经发展成为node.js的标准软件包管理工具(甚至与node一起提供!)。

If you have your own modules that you would like to maintain there are several ways to handle it. 如果您有自己想要维护的模块,则有几种处理方法。

Package specific modules 包特定的模块

(also sometimes called "libs") (有时也称为“库”)

The simplest way to write your own module is to require with relative or absolute path: 编写自己的模块的最简单方法是要求使用相对或绝对路径:

var my_module = require('./my_module'); // notice '.js' is not needed

Most people put all package specific modules in a directory called lib or src . 大多数人将所有软件包特定的模块放在一个名为libsrc的目录中。 So you can require them as: 因此,您可以要求它们为:

var my_module = require('./lib/my_module');

Note that the path is relative to the file you're editing. 请注意,该路径是相对于您正在编辑的文件的。 So if a lib file requires another lib file you don't need the '/lib' . 因此,如果一个lib文件需要另一个lib文件,则不需要'/lib'

Fancy package specific module 花式包装专用模块

Some people don't like the look of './lib/..' in require() . 有些人在require()不喜欢'./lib/..'的外观。 But they still want their package specific modules to be under the control of the package repo. 但是他们仍然希望其特定于软件包的模块在软件包回购的控制之下。 In which case one solution is to symlink the modules to node_modules . 在这种情况下,一种解决方案是将模块符号链接到node_modules

In one of my projects I have this in package.json: 在我的一个项目中,我在package.json中有以下内容:

"postinstall": "bash -c 'cd node_modules;ln -sf ../lib/*.js .'"

What it does is creates a symlink of all .js files inside ./lib to the node_modules folder when you run npm install . 它的作用是创建所有的符号链接.js中的文件./lib当您运行到node_modules文件夹npm install

Independent modules shard by several projects 独立模块由多个项目分片

If you have several modules shared by several of your projects you can (ab)use how node.js searches for modules. 如果您的多个项目共享多个模块,则可以(ab)使用node.js搜索模块的方式。 Node.js will search for the folder node_modules in the current directory and search for the module in there and if it can't find it it will recurse up the parent directory for a node_module folder etc. all the way to the root directory. Node.js将在当前目录中搜索文件夹node_modules在其中搜索模块,如果找不到它,它将递归node_module文件夹等的父目录,一直到根目录。

So, one way to have your own self-managed modules is to put a node_modules folder in the parent directory: 因此,拥有自己的自我管理模块的一种方法是将node_modules文件夹放在父目录中:

/home/myself/
    code/
        node_modules/
            my_module1.js
            my_module2/
                node_modules
                .git
                index.js
                package.json
        project1/
            node_modules/
            .git
            main.js
            package.json
        project2/
            node_modules/
            .git
            main.js
            package.json

In this way both project1 and project2 have access to my_module1 and my_module2 . 这样, project1project2都可以访问my_module1my_module2 In addition my_module2 can have its own git repo. 另外, my_module2可以有自己的git repo。

Late to the party but after reading several posts including this one and a few head bashing/testing hours later I feel compelled to share my take on a local repo under version control being used as a package in a node project. 参加聚会的时间很晚,但是在阅读了几篇文章(包括一篇以及几个小时的抨击/测试后)后,我感到不得不与我分享我在版本控制下用作节点项目中的软件包的本地仓库的看法。

Do this all from the root of your project: 从项目的根目录开始:

npm link /path/to/your/local/package/

npm link <"name:"> "name:" key in your local package's package.json npm link <"name:"> “ name:”在本地包的package.json中的键

npm install --save /path/to/your/local/package/

the last one you will not find in the npm documentation on using "link" but it's critical 您不会在npm文档中找到有关使用“链接”的最后一个,但这很关键

note : you can use relative paths (eg ../<localpackagefoldername> if sharing a parent folder) 注意 :您可以使用相对路径(例如,如果共享父文件夹, ../<localpackagefoldername>

important : your local package package.json MUST have a package "name:" key and a "main:" key pointing to an entry point js file otherwise this all fails. 重要 :您的本地软件包package.json必须有一个软件包“名称:”键和一个“ main:”键指向一个入口点js文件,否则所有操作都会失败。

now you can use require('name') in your code and later if you publish to npm you don't have to change anything expect the line in project package.json which would be just as easy to delete and npm install as edit. 现在,您可以在代码中使用require('name') ,以后再发布到npm时,您无需进行任何更改,可以在project package.json中找到该行,就像删除一样容易删除和npm install

If you add a package to your local module with npm install then do the same in your project and it will be added to the project's node_modules. 如果使用npm install将软件包添加到本地模块,则在项目中执行相同的操作,它将被添加到项目的node_modules中。 If you do an npm uninstall in your local package then do an npm prune in your project 如果您在本地软件包中进行npm uninstall ,则在项目中进行npm prune

note : if you have run npm install in your local package root then node_modules was created there for that package and now when you npm install in your project you'll get warnings like this you can ignore skippingAction Module is inside a symlinked module: not running remove . 注意 :如果已在本地软件包根目录中运行npm install在该软件包的根目录中创建了node_modules,现在当您在项目中进行npm install ,您将收到类似这样的警告,您可以忽略skippingAction Module is inside a symlinked module: not running remove If that bugs you then delete the node_modules folder in your local package if you are not running it separately. 如果那有问题,那么您可以在不单独运行本地软件包的情况下删除node_modules文件夹。

Starting with npm 3 node_modules are now flattened so doing it this way means your local package dependencies within the project are flattened too! 从npm 3开始,现在已对node_modules进行了展平,因此以这种方式进行操作意味着您在项目中对本地包的依赖也已展平! Further, you can't just put your full git repo into node_modules as npm will despise the .git folder and of course you'll be nesting your node_modules. 此外,您不能仅将完整的git repo放入node_modules中,因为npm会鄙视.git文件夹,当然您将嵌套node_modules。

My Big Trick : Sure you can have your local package repo in a directory separate from your project but if you make it a git submodule in a subdirectory of your project you get the best of both worlds, flattened dependencies in node_modules and combined pushes of the project and package (submodule) at the same time. 我的大把戏 :确保您可以在与项目不同的目录中拥有本地软件包存储库,但如果将其作为项目子目录中的git子模块,则可以兼得两全,node_modules中的扁平化依赖项以及项目和包(子模块)同时进行。 Plus it makes those link command paths trivial. 另外,它使那些链接命令路径变得微不足道。

Tip : If you go the separate directory (no submodule) route then use your ide editor (eg atom) to add the local package folder to your project tree for easy editing with your project. 提示 :如果您转到单独的目录(无子模块)路径,请使用ide编辑器(例如atom)将本地包文件夹添加到项目树中,以便于您的项目轻松编辑。 Too, if you go this route it's up to you to commit and push changes for the local package since it's not a submodule. 同样,如果您走这条路线,则由您来提交和推送对本地包的更改,因为它不是子模块。

Probably the only caveat I can think of at this time is to be sure that you have dependencies entries in your local package even if they are in the project's package.json otherwise if someone uses the local package somewhere else (on it's own) it will be missing dependencies for npm install 我可能目前唯一需要注意的警告是,即使本地软件包中有依赖项,也要确保它们在项目的package.json中,否则如果有人在其他地方(自己使用)使用本地软件包,它将缺少npm install依赖项

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

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