7

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

Javascript 源库

源库/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 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();
    }
}

公共 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-link :

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

角度测试应用

将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.

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

4

2 回答 2

2

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

{
  "projects": {
    "yourProject": {
      "architect": {
        "build": {
          "options": {
            "preserveSymlinks": true
          }
        }
      }
    }
  }    
}
于 2019-09-05T14:10:46.530 回答
0

请检查您要从中提取的包main中的参数。如果此类参数针对不存在的文件夹,则导入将失败package.jsonnpm link

于 2020-01-28T17:22:09.773 回答