Ellbar
Ellbar

Reputation: 4054

Angular library - Module not found: Error: Cannot resolve module

I have the simples example:

  1. I create Angular library inside workspace from official documentation:
ng new workspace-test --create-application=false
cd workspace-test
ng generate library lib-test
  1. Then I build library and I create npm link for it from ./dist/lib-test directory:
ng build lib-test
npm link
  1. Then I create simplest angular application:
ng new app-test
  1. Now I link my application to the library:
npm link lib-test
  1. When I try to use library inside my app, I get exception
 Error: Module not found: Error: Can't resolve '../../../workspace-test/dist/lib-test/lib/lib-test.component' in 'C:\dev\app-test\src\app'
  1. My usage of the library in the app:
import { LibTestModule } from '../../../workspace-test/dest/lib-test'


...


imports: [
   LibTestModule ,

I don't have idea what is going on, how to make it work? It basic example.

Upvotes: 0

Views: 5605

Answers (2)

Oni
Oni

Reputation: 11

You can install your missing module in node_module folder after building it.

Build your lib: cd my-lib ng build
Go to your workspace root: cd my-workspace
Then install via npm: npm i ./dist/my-lib

Upvotes: 1

Obada Saada
Obada Saada

Reputation: 78

if you want to use local library in another local project then your library build should be inside project's node_modules folder

solution change the library build destination to the project's node_modules

  • assume you have the following folders

  • go to my-lib>projects>my-lib>ng-package.json file

  • change dest to "../../../my-project/node_modules/my-lib"

  • build your library

Upvotes: 1

Related Questions