Reusing Angular 2 module in another application without publishing to npm?

Let's say you have two different Angular 2 applications and they need to use the same component.

I created a generic component, created a library following this guide http://blog.angular-university.io/how-to-create-an-angular-2-library-and-how-to-consume-it-jspm-vs -webpack / , but npm will make my code public, and I will have to pay to make it private.

So the questions are: 1. How can I create components that may be available to a workgroup? 2. Do I need to publish to npm or can I just push my code to private rehearsal github? And if so, how can I do this and what will be the process of reusing code in an application?

Thanks in advance.

+8
github module angular npm
source share
2 answers

package.json allows you to reference packages downloaded from the git repository, and this may be the one you use internally. See NPM Documentation

Formats example:

git+ssh:// git@github.com :npm/npm.git#v1.0.27 git+ssh:// git@github.com :npm/npm#semver:^5.0 git+https:// isaacs@github.com /npm/npm.git git://github.com/npm/npm.git#v1.0.27 

So this will be in your package. json give something like:

 "dependencies": { "privatepackage":"git://localgitserver/git/privatepackage.git" } 
+1
source share

I suggest you use NX for this, it is easy to clean up for exchanging code between applications

  • create an NX workspace

     npx --ignore-existing create-nx-workspace MY_WORK_SPACE 

Now you can create a multiplr application inside this workspace, it can be Angular or React App, or a combination of both.

  ng add @nrwl/angular --defaults ng g @nrwl/angular:application app1 // generate first app ng g @nrwl/angular:application app2 //generate second app 

they are visible in the application directory

enter image description here

so now you have your own applications, you can exchange code between applications by creating libraries

  ng g @nrwl/angular:lib ui // ui is the name of your library 

now available in the libs directory

enter image description here

Now you can work with your library, generating components and services:

 ng g component sharedComponent --project=ui --export 

Finally, you can use your library in your applications (app1 and app2) by importing your UiModule

 import { UiModule } from '@MY_WORK_SPACE/ui'; // @nameOfYourCreatedWorkspace/nameOfLibrary @NgModule({ declarations: [AppComponent], imports: [BrowserModule, HttpClientModule, UiModule], // import here providers: [], bootstrap: [AppComponent] }) export class AppModule {} 

and voila!

0
source share

All Articles