我正在尝试为 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.json 和 README.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/