简体   繁体   English

如何使用 npm 链接修复 Angular 库中的“未找到模块”错误?

[英]How to fix "Module not found" error in Angular library with npm link?

I'm trying to build an Angular wrapper for a Javascript library but I'm getting a "Module not found" error.我正在尝试为 Javascript 库构建一个 Angular 包装器,但出现“找不到模块”错误。 The Javascript library is in development and not yet published to NPM, so I'm using npm-link which might be causing the problem, but I'm not sure. Javascript 库正在开发中,尚未发布到 NPM,所以我使用的是 npm-link,这可能会导致问题,但我不确定。 My Angular version is 8.2.2.我的 Angular 版本是 8.2.2。

Javascript source library Javascript 源代码库

sourcelib/index.js:源库/index.js:

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

The sourcelib directory also contains package.json and README.md files. sourcelib目录还包含package.jsonREADME.md文件。 An npm-link is created as you would expect:如您所料,创建了一个 npm-link:

cd sourcelib
npm link

Angular wrapper角度包装器

Here are the Angular CLI commands I'm executing to create the Angular workspace, library and test application:以下是我正在执行以创建 Angular 工作区、库和测试应用程序的 Angular CLI 命令:

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

Now link testlib to the Javascript library:现在将testlib链接到 Javascript 库:

cd projects/testlib
npm link sourcelib

Generate a module and component inside testlib :testlib 中生成一个模块和组件:

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

testcomp.component.ts : 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 :公共 api.ts

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

Now build sourcelib :现在构建sourcelib

ng build

Here is the console output:这是控制台输出:

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

And create an npm-link to sourcelib :并创建一个指向sourcelib的 npm 链接:

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

Angular test app角度测试应用

Link testlib-app to testlib :testlib-app链接到testlib

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

app.module.ts : 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 : app.component.ts :

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

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

Serve the app:为应用程序提供服务:

ng serve

Result结果

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.

I've spent a lot of time troubleshooting this but have not been able to find a solution.我花了很多时间对此进行故障排除,但一直无法找到解决方案。 Any help would be appreciated.任何帮助,将不胜感激。

Angular sometimes has problems building with linked modules. Angular 有时会在构建链接模块时遇到问题。 Usually this can be fixed by adding "preserveSymlinks": true to your angular.json under projects.yourProject.architect.build.options .通常,这可以通过增加固定"preserveSymlinks": trueangular.jsonprojects.yourProject.architect.build.options For example:例如:

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

Please check the main param in the package.json of the package you want to npm link from.请检查要从中进行npm link的包的package.json中的main参数。 If such param is targeting a folder that doesnt exist, the import will fail如果此类参数针对不存在的文件夹则导入将失败

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM