Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. My Angular version is 8.2.2.

Javascript source library

sourcelib/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. An npm-link is created as you would expect:

cd sourcelib
npm link

Angular wrapper

Here are the Angular CLI commands I'm executing to create the Angular workspace, library and test application:

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:

cd projects/testlib
npm link sourcelib

Generate a module and component inside 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';

Now build 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:

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

Angular test app

Link testlib-app to 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 {
}

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.

like image 581
Elliot Avatar asked Sep 05 '19 13:09

Elliot


1 Answers

Angular sometimes has problems building with linked modules. Usually this can be fixed by adding "preserveSymlinks": true to your angular.json under projects.yourProject.architect.build.options. For example:

{
  "projects": {
    "yourProject": {
      "architect": {
        "build": {
          "options": {
            "preserveSymlinks": true
          }
        }
      }
    }
  }    
}
like image 123
UncleDave Avatar answered Oct 16 '22 16:10

UncleDave