简体   繁体   English

设置开发环境(node_modules复制)

[英]setting up development environment (node_modules duplication)

(I am an Actionscript developer finally migrating to JavaScript and am learning TypeScript and AngularJS 2 . Let me know if this question is too stupid/naive and I will delete it) (我是一个最终迁移到JavaScriptActionscript开发人员,正在学习TypeScriptAngularJS 2 。让我知道这个问题是否太愚蠢/天真,我将其删除)

I'm running through the Angular 2 tutorials and all is well but I am wondering about how one typically sets up a development environment without duplication of, for example the "node_modules" and "typings" directories. 我正在浏览Angular 2教程 ,一切都很好,但是我想知道人们通常如何设置开发环境而不重复“ node_modules”和“ typings”目录。 I understand that the tutorials want to be complete in themselves so there is duplication. 我了解这些教程希望自己完整,所以有重复之处。 I also get that, for example, the line below is using a reference relative to the project folder so it could point someplace else. 我也得到了例如,下面的行使用相对于项目文件夹的引用,因此它可以指向其他地方。

Is there a "best practice" for avoiding duplication of node modules? 是否有避免重复节点模块的“最佳实践”? I ask because the "node_modules" directory is 145 mb. 我问,因为“ node_modules”目录为145 mb。

import {Component} from 'angular2/core';

在此处输入图片说明

node_modules being duplicated in each project is by design - the idea of having a package.json file and a corresponding folder of dependencies is so that your project is self-sufficient/isolated and does not rely on the environment it's being used in. 每个项目中重复的node_modules是设计node_modules -具有package.json文件和相应的依赖文件夹的想法是,使您的项目能够自给自足/独立,并且不依赖于所使用的环境。

That said, you can install NPM modules globally using the npm install [package] -g command, but here's a practical example of why you probably shouldn't - say you have an ongoing project that uses an old version of Angular 2, and you want to start a second project using the latest features. 也就是说,您可以使用npm install [package] -g命令在全球范围内安装NPM模块,但这是一个实际示例,说明为什么您不应该这样做 -假设您有一个正在进行的项目正在使用旧版本的Angular 2,并且您想使用最新功能启动第二个项目。 If you're using a global Angular install, this isn't possible; 如果您正在使用全局Angular安装,则不可能。 updating the version would update it for every project that's using it (which could horribly break your old app). 更新版本会为正在使用它的每个项目更新它(这可能会严重破坏您的旧应用程序)。 If you have a separate install per-project, their versions can be different without causing any issues. 如果每个项目有单独的安装,则它们的版本可以不同,而不会引起任何问题。

This also comes in handy when committing your project to version control - if your entire app relies on globally installed modules and you (or a contributor) wants to work on it on another machine, they'll have to go through and install every single dependency manually so that the environment matches the one it was originally developed in. Compare this to having your dependencies listed in package.json and your node_modules folder excluded from the repo - they just have to run npm install and NPM will install all the relevant modules for them. 在将项目提交到版本控制中时,这也很方便-如果您的整个应用程序都依赖于全局安装的模块,而您(或贡献者)想在另一台计算机上使用它,则他们必须检查并安装每个依赖项手动进行操作,以使环境与最初开发的环境相匹配。这与将您的依赖项列在package.json以及将node_modules文件夹排除在node_modules之外–他们只需要运行npm install ,NPM就会为他们。

I admit that the size of node_modules can get excessive at times, and I hope they optimize this at some point - but I don't think this is a good reason to throw out all the benefits keeping it separate gives you! 我承认node_modules的大小node_modules可能会变node_modules大,我希望它们可以在某个时候对其进行优化-但我认为这不是扔掉将其分开给您带来所有好处的好理由!

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

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