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'

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Do you mean in the index.ts file? I have done: "export * from './myLibrary.module'"
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
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/*"
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.