如何在另一个 angular 项目的 package.json 中将 angular 项目设置为依赖项 [英] How to setup angular project as a dependency in package.json of another angular project
问题描述
我有三个不同的 Angular cli 项目(X、Y、Z).我想将 [X] 作为父项目,同时将 Y 和 Z 作为 npm 包依赖项添加到 X.这意味着 [X] package.json 将包含 [Y] 和 [Z] 的依赖项,如下所示.
依赖项":{"@angular/common": "^4.0.0",//.. 其他角度依赖"y": "1.0.1",z":1.0.3"}
如何创建这些依赖项?
注意:现在我将 Y 和 Z 作为 X 中的延迟加载文件夹.我想将其解耦为独立的 npm 包.
基本上你想通过 3 个步骤来做到这一点:
- 将您的项目y"和z"变成一个库
- 将该库打包到 npm 包中
- 让x"使用那个库
简而言之,您可以这样做:
我强烈建议使用 ng-packagr 来创建库y"和z".
ng-packagr
允许您将现有的Angular CLI
项目转换为库.基本上你必须做五件事:- 安装包:
npm install ng-packagr --save-dev
添加
<块引用>ng-package.json
,内容如下:<代码>{"$schema": "./node_modules/ng-packagr/ng-package.schema.json",库":{"entryFile": "public_api.ts"}}
将
<块引用>public_api.ts
添加到模块的导出中,即export * from './src/app/modules/example/example.module'
向
package.json
添加脚本:"packagr": "ng-packagr -p ng-package.json"
- 运行脚本:
npm run packagr
- 安装包:
通过运行
npm pack
创建 npm 包,这将生成一个y-1.0.0.tgz
文件,其中y
是您的项目名称,1.0.0
是您在package.json
中设置的版本现在你可以通过运行
npm install ./relative/path/to/y-1.0.0.tgz
把它作为依赖安装到你的项目x"中,你就完成了!
这篇文章基于这个 文章.
I have three different Angular cli projects (X, Y, Z). I want to make [X] as a parent project while I want to add Y and Z as npm package dependencies to X. That means [X] package.json will contain the dependencies of [Y] and [Z] as follows.
"dependencies": {
"@angular/common": "^4.0.0",
//.. other angular dependency
"y": "1.0.1",
"z": "1.0.3"
}
How to create those dependencies?
Note: Right now I have Y and Z as a lazy loaded folder inside X. Which I want to decouple as independent npm package.
Basically you want to do this in 3 steps:
- Turn your projects "y" and "z" into a library
- Pack that library into an npm-package
- Let "x" consume that library
Here is how you do it in short:
I highly recommend using ng-packagr for creating the library out of "y" and "z".
ng-packagr
allows you to take an existingAngular CLI
project and turn it into a library. Basically you have to do five things:- install package with:
npm install ng-packagr --save-dev
add
ng-package.json
with the following content:{ "$schema": "./node_modules/ng-packagr/ng-package.schema.json", "lib": { "entryFile": "public_api.ts" } }
add
public_api.ts
with the exports of your modules, i.e.export * from './src/app/modules/example/example.module'
add script to
package.json
:"packagr": "ng-packagr -p ng-package.json"
- run the script:
npm run packagr
- install package with:
Create the npm-package by running
npm pack
which will generate ay-1.0.0.tgz
-file, wherey
is your projects name and1.0.0
the version you set in yourpackage.json
Now you can install this as dependency in your project 'x' by running
npm install ./relative/path/to/y-1.0.0.tgz
and you're done!
This post is based on this article.
这篇关于如何在另一个 angular 项目的 package.json 中将 angular 项目设置为依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!