在Git子模块中使用NODE_PATH [英] Using NODE_PATH with Git submodules

查看:113
本文介绍了在Git子模块中使用NODE_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我询问了这个关于共享的问题代码之间的节点应用程序昨天没有答案,所以现在我有一个更具体的问题,而不是漫长的一个。



添加是不好的做法需要路径到NODE_PATH?



正如我在前一个问题中提到的,我正在努力找到一种方法来共享两个节点应用程序之间的公共资产,我能想出的唯一明智的解决方案是使用git子模块。我将这个包含公共资产(如Mongoose模式)的子模块放到这两个应用程序的根目录下,在必要时更新这些文件,然后将其推送到其他应用程序。这是一个非常简单的过程,没有与应用程序自己的文件发生严重合并问题,就像我在原始问题中想到的第三种解决方案。



这种结构的优点在于,我可以只用 folder / file.js 来请求这些公共资源子模块/文件夹/ file.js ,方法是将子模块路径添加到节点的要求路径。这也意味着我可以通过将子模块路径进一步放置在路径堆栈上来覆盖子模块的通用文件(如果它们存在的话),以便如果找到本地文件,节点将需要它而不是子模块的等价物。

但是有一个小问题。 Node的文档具有以下语句,其中包括向NODE_PATH添加要求的路径:


强烈建议您将您的依赖关系本地放置在node_modules文件夹中。它们将被加载得更快,更可靠。


所以基本上这意味着它被认为是一种糟糕的做法,并会降低应用程序的速度,真的不想要。或者这只适用于全局路径,所以子模块(它在应用程序的根中)不会是一个问题?

解决方案

我不建议添加路径到 NODE_PATH



在您的 package.json 中,您可以将模块直接链接到存储库url:

 dependencies:{
myModels:git + https:// user:pass@github.com/myAccount/my-models.git#v0.6
myTemplates:git + ssh://github.com/myAccount/my-templates.git#v0.2
}

正如您所看到的,您可以引用标签,分支或任何您想要的内容。您可以为每个应用程序使用不同的版本。只需更新您的标记,分支和 npm install



恕我直言,我不会使用 git submodules 你可以把事情搞糟很糟糕。在实际推动子模块之前,您可以(意外地)推动外部模块参考子模块,并且没有其他人能够工作,因为他们不会有仅在您的机器中提交的提交)。
此外,子模块总是以分离的HEAD模式签出并且有另外几个问题分支和合并



如果我的论点不足以让您不使用子模块,而是继续前进,而不是更新 NODE_PATH 您可以将一个代理文件添加到您的node_modules以避免相对路径: node_modules / models.js

  module.exports = require('../ lib / models'); 

这可能不是最好的做法,但您会避免所有模块的相对路径如果您在 .gitignore 下有 node_modules ,则必须省略这一个文件: / p>

  node_modules / * 
!node_modules / models.js

您可能希望将它添加到 package.json 中的 bundledDendencies 。是的,这有点麻烦,但你会避免相对路径,并能够将文件夹移动(太多)。



长话短说:将您的业务依赖关系添加到 package.json 可能就是您要查找的内容。


I asked this question about sharing code between Node apps yesterday with no answers, so now I have a more specific question instead of long-rambling one.

Is it considered a bad practice to add require paths to NODE_PATH?

As I mentioned in my previous question, I'm struggling with finding a way to share common assets between two node apps, and the only sensible solution I could come up with is using git submodules. I would place this submodule, containing common assets such as Mongoose schemas, to the both apps' root, update the files when necessary and then push 'n pull it over to the other app. It's a very simple process with no serious merging problems with the apps' own files like in the third solution I thought of in my original question.

The beauty of this structure would be that I could require those common assets with just folder/file.js instead of submodule/folder/file.js by adding the submodule path to Node's require paths. This would also mean that I could overwrite the submodule's common files with the app's own respective files (if they existed) by placing the submodule path further on the path stack, so that if a local file was found, node would require that instead of the submodule's equivalent.

But there's one tiny problem. Node's documentation has the following statement about adding require paths to NODE_PATH:

You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.

So basically this means it's considered a bad practice and slows down the app, which I really don't want. Or does this only apply to global paths, so the submodule (which is in the app's root) wouldn't be a problem?

解决方案

I wouldn't recommend to add paths to NODE_PATH.

In your package.json you can link modules directly to repositories url:

"dependencies": {
  "myModels": "git+https://user:pass@github.com/myAccount/my-models.git#v0.6"
  "myTemplates": "git+ssh://github.com/myAccount/my-templates.git#v0.2"
}

As you can see you can reference tags, branches or whatever you want. You can use different versions for each application. Just update your tag, branch and npm install.

IMHO, i wouldn't use git submodules you can mess things up pretty bad. You can (accidentally) push the outer module with a reference to the submodule before actually pushing the submodule and nobody else would be able to work since they wont have those commits that are only in your machine). Also, submodules are always checked out in detached HEAD mode and there are a few more issue regarding branching and merging.

If my argument is not good enough to make you not use submodules and you go ahead, instead of updating NODE_PATH you can just add a proxy file to your node_modules to avoid relative paths:

In node_modules/models.js:

module.exports = require('../lib/models');

This might not be the best thing to do but you'll avoid relative paths for that module all across the application.

If you have node_modules under .gitignore you'll have to omit this one file:

node_modules/*
!node_modules/models.js

And you might want to add it to bundledDendencies in package.json. Yeah, it's a bit messy but you'll avoid relative paths and be able to move folders around with messing things up (too much).

Long story short: Adding your business dependencies to package.json might be what you are looking for.

这篇关于在Git子模块中使用NODE_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆