15

I have created a project using angular-cli. There is one AppModule and AppComponent I want to use this AppModule and its components (AppComponent) in other angular apps. So I have created index.ts file and exported the AppModule and AppComponent

export {AppModule} from './src/app/app.module';
export {AppComponent} from './src/app/app.component';

Then created local linking using npm link and a link is created with the name defined in package.json.

Now In other project where I want to use this exported module run the

npm link project-name

Linking has been done successfully. I tried

import { AppModule as AModule} from 'my-components';

But this is not working as webpack gets failed to compiles AppModule file as the reference is not getting resolved. In SystemJs We defined the mapping of this in systemjs.config.js file but there is no config file.

How can I solve this?

Is there any other method to reuse local modules?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

6 Answers6

11

In Angular 7, "preserveSymlinks": true

It worked for me: project > architect > build > options

Diego Aragão
  • 111
  • 1
  • 2
9

Add the following to your tsconfig.json of the host application you are loading the npm-linked lib into.

    "paths": {
      "@angular/*": ["node_modules/@angular/*"]
    }

Apparently the linked packages can't use peer dependancies normally. This will force the library to use your app's node_modules/@angular/* libs, which is what happens when you npm install the lib.

Martin
  • 15,820
  • 4
  • 47
  • 56
3

It's working now with a "preserveSymlinks": true in your angular.json.

just add it there : project > architect > build.

Yoann Augen
  • 1,966
  • 3
  • 21
  • 39
1

this is a hot topic at the moment which seems not yet soloved completey. I got to the following information:

Linking lib created by the angular cli ist not that easy. You can read in the docs

https://github.com/angular/angular-cli/wiki/stories-linked-library

I was able to make it work by following the comment provided by nimaen in this post https://github.com/angular/angular-cli/issues/3875

--BR Dennis

1

You wont be able. Let me quote

We don't support Library building with the CLI right now. We do support linking libraries built properly inside a CLI application.

https://github.com/angular/angular-cli/issues/9273

aemonge
  • 2,289
  • 1
  • 24
  • 26
  • 1
    Take a look here. I think the tgz alternative is much less of a headache. at least for the most part it seems to have less issues. https://dev.to/vishesh/creating-new-angular-library-and-publishing-it-in-npm-5f3g – Decoded Dec 08 '20 at 06:53
  • 1
    This answer is no longer valid – Martin Dec 16 '21 at 19:14
0

You can drop the links to the projects to tsconfig.js file for any modules that you want to work in the source code form.

Note that the module alias must be a pointer to a TypeScript file without ts or js extension, not to the directory of package.json file.

{
  "compilerOptions": {
    ...
    "paths": {
      "my-third-party-package": [
        // Can be project root relative path or abs path on your hd
        "projects/my-third-party-package/src/index"
      ],
    }
  },
}
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435