javascript - 如何使用 npm 链接修复 Angular 库中的 "Module not found"错误?

标签 javascript angular typescript npm npm-link

我正在尝试为 Javascript 库构建 Angular 包装器,但出现“找不到模块”错误。 Javascript 库正在开发中,尚未发布到 NPM,因此我使用的 npm-link 可能会导致问题,但我不确定。我的 Angular 版本是 8.2.2。

Javascript源码库

sourcelib/index.js:

var sourcelib = (function () {
    var doSomething = function() {
    };
    return {
        doSomething: doSomething
    };
})();
export default sourcelib;

sourcelib 目录还包含 package.jsonREADME.md 文件。一个 npm-link 如你所料创建:

cd sourcelib
npm link

Angular 包装器

以下是我为创建 Angular 工作区、库和测试应用程序而执行的 Angular CLI 命令:

ng new app-test --create-application=false
cd app-test
ng generate library testlib
ng generate application testlib-app

现在将 teSTLib 链接到 Javascript 库:

cd projects/testlib
npm link sourcelib

teSTLib中生成一个模块和组件:

cd src/lib
ng generate module test
ng generate component test/testcomp

testcomp.component.ts:

import { Component, OnInit } from '@angular/core';
import sourcelib from 'sourcelib';

@Component({
    selector: 'testcomp',
    template: '<div></div>'
})
export class TestcompComponent implements OnInit {
    constructor() { }

    ngOnInit() {
        sourcelib.doSomething();
    }
}

public-api.ts:

export * from './lib/test/test.module';
export * from './lib/test/testcomp/testcomp.component';

现在构建sourcelib:

ng build

这是控制台输出:

Building Angular Package

------------------------------------------------------------------------------
Building entry point 'testlib'
------------------------------------------------------------------------------
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
WARNING: No name was provided for external module 'sourcelib' in output.globals – guessing 'sourcelib'
Minifying UMD bundle
Copying declaration files
Writing package metadata
Built testlib

------------------------------------------------------------------------------
Built Angular Package!
 - from: /Users/.../app-test/projects/testlib
 - to:   /Users/.../app-test/dist/testlib
------------------------------------------------------------------------------

并创建一个指向 sourcelib 的 npm 链接:

cd ../../dist/testlib
npm link

Angular Testing 应用

teSTLib-app 链接到 teSTLib:

cd ../../projects/testlib-app
npm link testlib

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { TestcompComponent } from 'testlib';

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

app.component.ts:

import { Component } from '@angular/core';
import { TestcompComponent } from 'testlib';

@Component({
    selector: 'app-root',
    template: '<testcomp></testcomp>'
})
export class AppComponent {
}

为应用提供服务:

ng serve

结果

ERROR in /Users/.../app-test/dist/testlib/fesm2015/testlib.js
Module not found: Error: Can't resolve 'sourcelib' in '/Users/.../app-test/dist/testlib/fesm2015'
ℹ 「wdm」: Failed to compile.

我花了很多时间对此进行故障排除,但未能找到解决方案。任何帮助将不胜感激。

最佳答案

Angular 有时在使用链接模块构建时会出现问题。通常这可以通过将 "preserveSymlinks": true 添加到 projects.yourProject.architect.build.options 下的 angular.json 来解决。例如:

{
  "projects": {
    "yourProject": {
      "architect": {
        "build": {
          "options": {
            "preserveSymlinks": true
          }
        }
      }
    }
  }    
}

关于javascript - 如何使用 npm 链接修复 Angular 库中的 "Module not found"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57806126/

相关文章:

angular - typescript 错误 :- Property 'forEach' does not exist on type 'string'

javascript - onclick 需要 3 次点击才能完成事件

AngularFire根据用户身份验证检索文档ID

angular - 在 docker 内使用 chrome headless 运行 ng 测试(angular-cli 业力测试)

javascript - AngularJS 到 Angular - NgUpgrade

angular - 何时在指令 @Inputs 中使用方括号 [ ] 何时不用?

javascript - 在对象数组中查找 angular 6/typescript/javascript

javascript - 用于单元测试的 LoopbackJS 注册模型

javascript - Zurb Foundation 4 显示模态 : Won't close

javascript - for 循环在自动完成内不起作用