简体   繁体   English

Angular CLI - 使用“ng new library”创建的自定义库时“找不到模块”

[英]Angular CLI - 'Cannot find module' when using custom library created with 'ng new library'

I'm trying to consume my own library after creating it with the Angular CLI using ng new library lib-name .我正在尝试使用 Angular CLI 使用ng new library lib-name创建我自己的库。

I'm trying to import the library as documented, this way:我正在尝试按记录导入库,这样:

import {MyLibModule} from 'ngx-mylib';

But I get the following error:但我收到以下错误:

 error TS2307: Cannot find module 'ngx-mylib'.

I did ng build ngx-mylib and also verified my tsconfig.json has the following paths (which are added automatically using the CLI):我做了ng build ngx-mylib并且还验证了我的 tsconfig.json 具有以下路径(使用 CLI 自动添加):

"paths": {
      "ngx-mylib": [
        "dist/ngx-chartjs"
      ],
      "ngx-mylib/*": [
        "dist/ngx-chartjs/*"
      ]
    }

public-api.ts:公共api.ts:

export * from './lib/charts.service';
export * from './lib/chart.component';
export * from './lib/charts.module';

What I'm missing?我错过了什么?

After build your library you have to pack it.构建库后,您必须打包它。

Go to your dist folder and run Go 到您的 dist 文件夹并运行

npm pack

this will create a your-lib-name.tgz file and then you can use it on your projects.这将创建一个 your-lib-name.tgz 文件,然后您可以在您的项目中使用它。

Use it like this:像这样使用它:

npm install path-to-your-tgz-file/your-lib.tgz

You can also publish it to npm or to a private repository.您还可以将其发布到 npm 或私有存储库。

try to build it again and then run your project sometimes angular can't build the library properly尝试再次构建它然后运行您的项目有时 angular 无法正确构建库

Try adding MyLibModule to your app.module imports and exports.尝试将 MyLibModule 添加到您的 app.module 导入和导出中。

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

相关问题 Angular 找不到我的自定义库的模块 - Angular Cannot find module of my custom library 如何删除使用 CLI 创建的 Angular 库(ng generate library) - How to remove an Angular library created with CLI (ng generate library) 使用SystemJS将ng2-ace库集成到新创建的angular-cli(angular2)项目中 - Integrate the ng2-ace library into a freshly created angular-cli (angular2) project using SystemJS 将 SCSS 与使用 angular-cli 创建的角度库一起使用 - Using SCSS with angular library created using angular-cli 打字稿:无法找到我的自定义库的模块 - Typescript: Cannot find module of my custom library 在 ng serve 找不到模块“@angular/compiler-cli/ngcc” - Cannot find module '@angular/compiler-cli/ngcc' at ng serve 运行 ng serve 时找不到模块“@angular/compiler-cli/ngc” - cannot find module '@angular/compiler-cli/ngc' when I run ng serve 在我的应用程序上运行“ng serve”时“找不到模块‘@angular/compiler-cli/ngcc’” - "Cannot find module '@angular/compiler-cli/ngcc'" when running "ng serve" on my app 如何在使用CLI创建的Angular库中包含第三方模块 - How to include third party modules in an Angular library created using the CLI 使用“ng generate library”时什么是Angular“Library”? - When using "ng generate library" what is an Angular "Library"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM