4

I have made my first library, using the new angular-cli command: library.

After I build the library I wanted to import it into an existing project (without publishing to npm), but unfortunately without luck.. Actually, it works if I copy it into node_modules, but then I would have to do that every time I update my node_modules folder. Therefore I would like to have a folder (libs) in the root directory where I can store and reference all my (homemade) libraries.

I tried by making the folder and pasting the library into it, and added the paths options in tsconfig. Which results in a "cannot find module" error (I guess I am missing something in the cli setup, but I cannot figure out what)

Do you know what I am missing? Or do you have another approach to this scenario?

Thanks :)

EDIT:

tsconfig.json:

{
 "compilerOptions": {
 // code omitted for brevity
  "paths": {
    "my-lib": ["./libs/my-lib"],
  }
}

import in app.module.ts

import { MyLibraryModule } from 'my-lib'
Beese
  • 495
  • 2
  • 5
  • 19

3 Answers3

3

Okay so I found a solution. I saved the libraries in the libs folder, and did "npm install /absolute/path/to/my/library --save" which seems to be the way to do it. (This creates a reference to the local library instead of npm)

Hope it helps anyone out there ;)

Beese
  • 495
  • 2
  • 5
  • 19
0

Did you also export "./libs/my-lib" to make it available ?

Joel Wembo
  • 814
  • 6
  • 10
  • Do you mean in the index.ts file? I have done: "export * from './myLibrary.module'" – Beese Jun 18 '18 at 14:11
  • angular 2+ always require to add the component both on Ngmodule and export appmodule. you probably missing to import on app.module or you didnt export correctly. It is a common error, you will fix it – Joel Wembo Jun 18 '18 at 16:17
0

In my case the problem was that my baseUrl was "src" and my "paths" config was

"ngx-y-x": [
  "dist/ngx-y-x"
],
"ngx-y-x/*": [
  "dist/ngx-y-x/*"
]

So from "src" you try to find "dist" which is not there - I have to go back one level.

"ngx-y-x": [
  "../dist/ngx-y-x"
],
"ngx-y-x/*": [
  "../dist/ngx-y-x/*"
]
Vallerious
  • 570
  • 6
  • 13