【问题标题】:Error using ng-packagr : Module not found: Error: Can't resolve使用 ng-packagr 时出错:找不到模块:错误:无法解析
【发布时间】:2019-02-14 06:42:08
【问题描述】:

我无法导入我在主项目中打包和引用的子模块之一中定义的组件。一切似乎都很好,但我仍然看到一个错误 -

./src/app/app.module.ts 中的错误找不到模块:错误:不能 解决'@first-project/second-project/app/regist 配给/registration.component'在 'C:\Users\anujin\Documents\workspace-vs-code\ FirstProject\src\app' ./src/app/app-routing.module.ts 中的错误找不到模块:错误: 无法解析 '@first-project/second-project/app/regist 配给/registration.component'在 'C:\Users\anujin\Documents\workspace-vs-code\FirstProject\src\app'

我创建了两个项目 -

  • 第一个项目
  • 第二个项目

我在SecondProject 中创建了一个RegistrationComponent,我需要在FirstProject 中使用它。

FirstProject 文件:

package.json

{
  "name": "@first-project/second-project",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "packagr": "ng-packagr -p ng-package.json"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "core-js": "^2.5.4",
    "ng-packagr": "^4.7.0",
    "rxjs": "~6.3.3",
    "tsickle": "^0.34.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.1",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

ng-package.json-

{
    "$schema": "./node_modules/ng-packagr/package.schema.json",
    "lib": {
        "entryFile": "./src/public_api.ts"
    },
    "whitelistedNonPeerDependencies": [
        "@angular/animations",
        "@angular/cdk",
        "@angular/common",
        "@angular/compiler",
        "@angular/forms",
        "@angular/core",
        "@angular/http",
        "@angular/material",
        "@angular/platform-browser",
        "@angular/platform-browser-dynamic",
        "@angular/router",
        "core-js",
        "rxjs",
        "tsickle",
        "zone.js",
        "ng-packagr"
      ]
}

public_api.ts

export * from './app/app.module';
export * from './app/registration/registration.component';

我在FirstProject 中运行了以下命令 -

  • npm 运行打包程序
  • cd 分配
  • npm 包

然后我在FirstProject中引用了打包的SecondProject

FirstProject 文件 -

package.json

{
  "name": "first-project",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/cdk": "~7.3.1",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/material": "^7.3.1",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "core-js": "^2.5.4",
    "hammerjs": "^2.0.8",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26",
    "@first-project/second-project": "<path>/SecondProject/dist/first-project-second-project-0.0.0.tgz"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.1",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { RegistrationComponent } from '@first-project/second-project/app/registration/registration.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';


@NgModule({
  declarations: [
    AppComponent,
    RegistrationComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { RegistrationComponent } from '@first-project/second-project/app/registration/registration.component';

const routes: Routes = [
  {path: 'registration', component: RegistrationComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我在FirstProject 中做了npm install,我看到节点模块下的引用正确创建,但我在FirstProject 中做了npm start,它给出了以下错误 -

./src/app/app.module.ts 中的错误 找不到模块:错误:无法解析'@first-project/second-project/app/regist 'C:\Users\anujin\Documents\workspace-vs-code\ 中的 ration/registration.component' FirstProject\src\app' ./src/app/app-routing.module.ts 中的错误 找不到模块:错误:无法解析'@first-project/second-project/app/regist 'C:\Users\anujin\Documents\workspace-vs-code\ 中的 ration/registration.component' FirstProject\src\app'

我无法理解我在这里缺少什么。

【问题讨论】:

    标签: angular ng-packagr


    【解决方案1】:

    您可以使用共享文件夹,在共享模块中创建 RegistrationComponent 并在两个项目中使用它。请参考以下链接,它将解决您的问题。

    Share code(component) between multiple projects

    Code-Sharing-with-multiple-app-angular-project-using-base-components

    【讨论】:

    • 您的链接给了我一个寻找解决方案的方向。谢谢一堆。上投票。干杯!!
    【解决方案2】:

    请将您的组件导出到 public.ts 文件中

    public_api.ts
    export * from './app/RegistrationComponent.component';
    export * from './app/app.module';
    

    对于组件: 使用导出使元素可见。将其添加到 入口文件使类可见。

    阅读:The Angular Library Series - Creating a Library with Angular CLI

    【讨论】:

    • 同样的错误。我也更新了public_api.ts。我之前确实尝试过,但仍然遇到同样的错误。现在再试一次,发现同样的错误。
    • @apun - 你还需要导出你的注册模块
    • 但这是一个组件,而不是我已经导出的模块。不是吗?使用ng-cli生成项目时,默认只有一个应用程序模块@
    • @apun - 你的代码不清楚,即使在你的问题中也无法找到哪个是第一个项目,哪个是第二个,如果你可以共享代码,那么我可以提供更多帮助
    • 好的,请给我一分钟。分享吧。
    【解决方案3】:

    实际上在 dist 文件夹中生成的 package.json 缺少 'main' 字段,它暴露了库的默认 .js 文件。 要检查这一点,您可以手动添加它。在使用该库的应用程序的 node_modules 中,打开库包并在 package.json 中添加如下内容: "main": "./esm2015/my-library-name-here.js"。当然,这取决于您的库的名称。 我还没有找到使用 ng-packagr 配置自动定义它的明确方法,所以我正在做一个自制的脚本!

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 2018-12-11
      • 2019-02-21
      • 2018-06-29
      • 1970-01-01
      • 2020-12-15
      • 2017-12-29
      相关资源
      最近更新 更多