简体   繁体   English

在不同项目之间共享 node_modules

[英]Share node_modules between different projects

I'm developing various Angular 2 projects and I want to share node_modules folder between multiple projects.我正在开发各种 Angular 2 项目,我想在多个项目之间共享 node_modules 文件夹。 I would like to create a structure like this:我想创建一个这样的结构:

MainFolder
- Project1
- Project2
- package.json

so I would have just 1 package.json for all the projects.所以我所有的项目只有 1 个 package.json。 My answer: is it possible to do this?我的回答:可以这样做吗? If possible, I have to lunch npm install with -g ?如果可能,我必须使用-g来进行npm install午餐? I can't understand how -g works.我无法理解-g是如何工作的。 Can someone give me instructions how to proceed?有人可以给我指示如何继续吗? Very thanks很感谢

I forgot to say that I build the projects with angular-cli.我忘了说我用 angular-cli 构建项目。

The way I go around this for small/learning/test projects is with (I call it) "git projects".我为小型/学习/测试项目解决这个问题的方法是(我称之为)“git 项目”。 Basically I manage the various projects via git, and just "load" the project I want to work on.基本上我通过 git 管理各种项目,并且只是“加载”我想要处理的项目。 Of course this doesn't work if you want to have access to multiple projects at the same time.当然,如果您想同时访问多个项目,这将不起作用。

I like to use a git client for this purpose because it's easier to visualize my existing "projects".我喜欢为此目的使用 git 客户端,因为它更容易可视化我现有的“项目”。

So my workflow is this...所以我的工作流程是这样的......

  1. Create my main/base folder.创建我的主/基本文件夹。 This will contain the git repo, the single node_modules folder, and whatever else that should be common to all projects.这将包含 git repo、单个 node_modules 文件夹,以及所有项目通用的任何其他内容。
  2. I create the basic package.json file (using npm init ).我创建了基本的 package.json 文件(使用npm init )。 No description, no nothing, just the basic skeleton package.json file.没有描述,什么也没有,只是基本的骨架 package.json 文件。 (However, if you know you will use certain packages in ALL of your projects, you can npm install them first, so they will be added to package.json as your "base" modules.) (但是,如果你知道你将在所有项目中使用某些包,你可以先 npm install 它们,所以它们将被添加到 package.json 作为你的“基本”模块。)
  3. Now I check the bare package.json into the repo (and anything else that you may want to have in all of your projects, but usually it's just the package.json file).现在,我将裸 package.json 检入到 repo(以及您可能希望在所有项目中拥有的任何其他内容,但通常只是 package.json 文件)。 This will be the bare-bones starting branch for all projects.这将是所有项目的基本启动分支。
  4. Once this is checked in, I create a branch off of this in the git repo.签入后,我会在 git repo 中创建一个分支。 This will be "Project 1" - or whatever you want to call it.这将是“项目 1” - 或者您想称呼它的任何名称。 Then build up your project however you want, installing modules, checking in changes, etc, etc.然后根据需要构建您的项目,安装模块,检查更改等。
  5. When I want to start a new project, I simply check out the first bare-bones project (which is just the empty, or almost empty, package.json file) and do another branch off of it.当我想开始一个新项目时,我只需检查第一个基本项目(它只是空的或几乎是空的 package.json 文件),然后再做一个分支。 This will be my 2nd project.这将是我的第二个项目。

And so forth...等等……

So the main thing is that every new "project" will be a new branch in the git repo, and to create a new project, just switch back to the original bare-bones one and do a new branch off of that.所以最重要的是,每个新的“项目”都将成为 git 存储库中的一个新分支,要创建一个新项目,只需切换回原始的准系统,然后再创建一个新分支即可。

Of course it is possible to create branches within a project, too.当然,也可以在项目中创建分支。 It's all about naming conventions.这都是关于命名约定的。 You could, for example, prefix a new project branch with "P_" or "PROJECT_", etc, so you can quickly tell in your git client which branches are projects.例如,您可以在新项目分支前添加“P_”或“PROJECT_”等前缀,这样您就可以在 git 客户端中快速判断哪些分支是项目。 And of course use a different naming scheme if you just need a new branch within an existing project.如果您只需要现有项目中的新分支,当然可以使用不同的命名方案。 That's basically how I go about it.这基本上就是我的做法。

You may not like this workflow, but this way I don't need to install packages globally.你可能不喜欢这个工作流程,但这样我就不需要全局安装包。 When I do a backup, I can simply delete the single (possibly huge) node_modules folder.当我做备份时,我可以简单地删除单个(可能很大)的 node_modules 文件夹。 All project related modules can be reinstalled by simply checking out a branch for a particular project and run " npm install " on its package.json.所有与项目相关的模块都可以通过简单地检出特定项目的分支并在其 package.json 上运行“ npm install ”来重新npm install Hope it makes sense.希望这是有道理的。

Here is documentation on the various npm install arguments是有关各种 npm install 参数的文档

In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.在全局模式下(即在命令后附加 -g 或 --global),它将当前包上下文(即当前工作目录)安装为全局包。

The -g install locations based on environment can be found here可以在此处找到基于环境的 -g 安装位置

One way you can achieve what you want is to have one solution for both projects and each project route uses it's own lazy loaded module .您可以实现所需的一种方法是为两个项目提供一个解决方案,并且每个项目路线都使用它自己的延迟加载模块

Unless you have a specific business need to share resources, it's better to keep each project separate with it own resources and configuration.除非您有特定的业务需要共享资源,否则最好将每个项目与它自己的资源和配置分开。

-g Stands for global Installation, ie the packages you install will be available for all applications. -g 代表全局安装,即您安装的软件包可用于所有应用程序。

And why do you want to share node_modules and package.json file?为什么要共享 node_modules 和 package.json 文件?

Keep them seperate for each seperate project.对于每个单独的项目,将它们分开。 And if you need to share your project, you may share your package.json instead of sharing the node_modules folder.如果您需要共享您的项目,您可以共享您的 package.json 而不是共享 node_modules 文件夹。

Also to point out, if you manually install packages by listing their names, then you can use -g (global) flag, but if you do use only npm install then your packages won't be installed as global packages.还要指出的是,如果您通过列出软件包的名称手动安装软件包,那么您可以使用 -g(全局)标志,但是如果您只使用npm install那么您的软件包将不会作为全局软件包安装。

If it really is just for testing simple applications, could rename tha app folder in some way provide a solution.如果真的只是为了测试简单的应用程序,可以通过某种方式重命名应用程序文件夹提供解决方案。 It assumes that all the dependencies are the same or at least a subset of the dependencies provided.它假设所有的依赖项都是相同的,或者至少是所提供的依赖项的一个子集。

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

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