Pack/Import a local developed module into a project

后端 未结 1 934
情深已故
情深已故 2021-02-08 19:35

I am trying to import a locally developed Angular project/module into an angular application without publishing it into npm repository.

First, I followed this tutorial t

1条回答
  •  有刺的猬
    2021-02-08 20:14

    I found this article that helped me to solve my problem:

    https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e


    To give a brief summary, this is how I proceeded:

    1. Install ng-packagr:
      • Install it globally:
        npm install -g ng-packagr
      • Install it in the project module:
        npm install ng-packagr --save-dev
    2. create ng-package.json in the root folder of the project, and add the following:
      {
        "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
        "lib": {
          "entryFile": "public_api.ts"   
          "externals": {
            "@angular/cdk": "ng.cdk",
            "@angular/cdk/accordion": "ng.cdk.accordion",
             //...
          }
        }
      }

    In my case I had to add external dependencies in order to avoid packaging/build errors:

    1. create public_api.ts in the root folder of the project, and add the following:

      export * from './src/app/modules/myFeature/my-feature.module'

    2. Edit package.json, and add the packagr line to the scripts tag:

    "scripts": {
      //...
      "packagr": "ng-packagr -p ng-package.json"
    }

    1. Create the package by running the following command from the root folder:

      npm run packagr

    2. Install it for local development:

      • Pack the module by running the following command from the dist folder:
        npm pack
      • Install the packed module from the final project:
        npm install ../some-relative-path/dist/my-component-library-0.0.0.tgz

    Then I could import my module from any other module or component of my final project

    0 讨论(0)
提交回复
热议问题