|
| 1 | +# Usage of Angular libraries published to npm |
| 2 | + |
| 3 | +When you build your Angular application, take advantage of sophisticated first-party libraries, as well as a rich ecosystem of third-party libraries. |
| 4 | +[Angular Material][AngularMaterialMain] is an example of a sophisticated first-party library. |
| 5 | + |
| 6 | +## Install libraries |
| 7 | + |
| 8 | +Libraries are published as [npm packages][GuideNpmPackages], usually together with schematics that integrate them with the Angular CLI. |
| 9 | +To integrate reusable library code into an application, you need to install the package and import the provided functionality in the location you use it. |
| 10 | +For most published Angular libraries, use the `ng add <lib_name>` Angular CLI command. |
| 11 | + |
| 12 | +The `ng add` Angular CLI command uses a package manager to install the library package and invokes schematics that are included in the package to other scaffolding within the project code. |
| 13 | +Examples of package managers include [npm][NpmjsMain] or [yarn][YarnpkgMain]. |
| 14 | +Additional scaffolding within the project code includes import statements, fonts, and themes. |
| 15 | + |
| 16 | +A published library typically provides a `README` file or other documentation on how to add that library to your application. |
| 17 | +For an example, see the [Angular Material][AngularMaterialMain] documentation. |
| 18 | + |
| 19 | +### Library typings |
| 20 | + |
| 21 | +Typically, library packages include typings in `.d.ts` files; see examples in `node_modules/@angular/material`. |
| 22 | +If the package of your library does not include typings and your IDE complains, you might need to install the `@types/<lib_name>` package with the library. |
| 23 | + |
| 24 | +For example, suppose you have a library named `d3`: |
| 25 | + |
| 26 | +<docs-code language="shell"> |
| 27 | + |
| 28 | +npm install d3 --save |
| 29 | +npm install @types/d3 --save-dev |
| 30 | + |
| 31 | +</docs-code> |
| 32 | + |
| 33 | +Types defined in a `@types/` package for a library installed into the workspace are automatically added to the TypeScript configuration for the project that uses that library. |
| 34 | +TypeScript looks for types in the `node_modules/@types` directory by default, so you do not have to add each type package individually. |
| 35 | + |
| 36 | +If a library does not have typings available at `@types/`, you may use it by manually adding typings for it. |
| 37 | +To do this: |
| 38 | + |
| 39 | +1. Create a `typings.d.ts` file in your `src/` directory. |
| 40 | + This file is automatically included as global type definition. |
| 41 | + |
| 42 | +1. Add the following code in `src/typings.d.ts`: |
| 43 | + |
| 44 | + <docs-code language="typescript"> |
| 45 | + |
| 46 | + declare module 'host' { |
| 47 | + export interface Host { |
| 48 | + protocol?: string; |
| 49 | + hostname?: string; |
| 50 | + pathname?: string; |
| 51 | + } |
| 52 | + export function parse(url: string, queryString?: string): Host; |
| 53 | + } |
| 54 | + |
| 55 | + </docs-code> |
| 56 | + |
| 57 | +1. In the component or file that uses the library, add the following code: |
| 58 | + |
| 59 | + <docs-code language="typescript"> |
| 60 | + |
| 61 | + import * as host from 'host'; |
| 62 | + const parsedUrl = host.parse('https://angular.dev'); |
| 63 | + console.log(parsedUrl.hostname); |
| 64 | + |
| 65 | + </docs-code> |
| 66 | + |
| 67 | +Define more typings as needed. |
| 68 | + |
| 69 | +## Updating libraries |
| 70 | + |
| 71 | +A library is able to be updated by the publisher, and also has individual dependencies which need to be kept current. |
| 72 | +To check for updates to your installed libraries, use the [`ng update`][CliUpdate] Angular CLI command. |
| 73 | + |
| 74 | +Use `ng update <lib_name>` Angular CLI command to update individual library versions. |
| 75 | +The Angular CLI checks the latest published release of the library, and if the latest version is newer than your installed version, downloads it and updates your `package.json` to match the latest version. |
| 76 | + |
| 77 | +When you update Angular to a new version, you need to make sure that any libraries you are using are current. |
| 78 | +If libraries have interdependencies, you might have to update them in a particular order. |
| 79 | +See the [Angular Update Guide][AngularUpdateMain] for help. |
| 80 | + |
| 81 | +## Adding a library to the runtime global scope |
| 82 | + |
| 83 | +If a legacy JavaScript library is not imported into an application, you may add it to the runtime global scope and load it as if it was added in a script tag. |
| 84 | +Configure the Angular CLI to do this at build time using the `scripts` and `styles` options of the build target in the [`angular.json`][GuideWorkspaceConfig] workspace build configuration file. |
| 85 | + |
| 86 | +For example, to use the [Bootstrap 4][GetbootstrapDocs40GettingStartedIntroduction] library |
| 87 | + |
| 88 | +1. Install the library and the associated dependencies using the npm package manager: |
| 89 | + |
| 90 | + <docs-code language="shell"> |
| 91 | + |
| 92 | + npm install jquery --save |
| 93 | + npm install popper.js --save |
| 94 | + npm install bootstrap --save |
| 95 | + |
| 96 | + </docs-code> |
| 97 | + |
| 98 | +1. In the `angular.json` configuration file, add the associated script files to the `scripts` array: |
| 99 | + |
| 100 | + <docs-code language="json"> |
| 101 | + |
| 102 | + "scripts": [ |
| 103 | + "node_modules/jquery/dist/jquery.slim.js", |
| 104 | + "node_modules/popper.js/dist/umd/popper.js", |
| 105 | + "node_modules/bootstrap/dist/js/bootstrap.js" |
| 106 | + ], |
| 107 | + |
| 108 | + </docs-code> |
| 109 | + |
| 110 | +1. Add the `bootstrap.css` CSS file to the `styles` array: |
| 111 | + |
| 112 | + <docs-code language="css"> |
| 113 | + |
| 114 | + "styles": [ |
| 115 | + "node_modules/bootstrap/dist/css/bootstrap.css", |
| 116 | + "src/styles.css" |
| 117 | + ], |
| 118 | + |
| 119 | + </docs-code> |
| 120 | + |
| 121 | +1. Run or restart the `ng serve` Angular CLI command to see Bootstrap 4 work in your application. |
| 122 | + |
| 123 | +### Using runtime-global libraries inside your app |
| 124 | + |
| 125 | +After you import a library using the "scripts" array, do **not** import it using an import statement in your TypeScript code. |
| 126 | +The following code snippet is an example import statement. |
| 127 | + |
| 128 | +<docs-code language="typescript"> |
| 129 | + |
| 130 | +import * as $ from 'jquery'; |
| 131 | + |
| 132 | +</docs-code> |
| 133 | + |
| 134 | +If you import it using import statements, you have two different copies of the library: one imported as a global library, and one imported as a module. |
| 135 | +This is especially bad for libraries with plugins, like JQuery, because each copy includes different plugins. |
| 136 | + |
| 137 | +Instead, run the `npm install @types/jquery` Angular CLI command to download typings for your library and then follow the library installation steps. |
| 138 | +This gives you access to the global variables exposed by that library. |
| 139 | + |
| 140 | +### Defining typings for runtime-global libraries |
| 141 | + |
| 142 | +If the global library you need to use does not have global typings, you can declare them manually as `any` in `src/typings.d.ts`. |
| 143 | + |
| 144 | +For example: |
| 145 | + |
| 146 | +<docs-code language="typescript"> |
| 147 | + |
| 148 | +declare var libraryName: any; |
| 149 | + |
| 150 | +</docs-code> |
| 151 | + |
| 152 | +Some scripts extend other libraries; for instance with JQuery plugins: |
| 153 | + |
| 154 | +<docs-code language="typescript"> |
| 155 | + |
| 156 | +$('.test').myPlugin(); |
| 157 | + |
| 158 | +</docs-code> |
| 159 | + |
| 160 | +In this case, the installed `@types/jquery` does not include `myPlugin`, so you need to add an interface in `src/typings.d.ts`. |
| 161 | +For example: |
| 162 | + |
| 163 | +<docs-code language="typescript"> |
| 164 | + |
| 165 | +interface JQuery { |
| 166 | + myPlugin(options?: any): any; |
| 167 | +} |
| 168 | + |
| 169 | +</docs-code> |
| 170 | + |
| 171 | +If you do not add the interface for the script-defined extension, your IDE shows an error: |
| 172 | + |
| 173 | +<docs-code language="text"> |
| 174 | + |
| 175 | +[TS][Error] Property 'myPlugin' does not exist on type 'JQuery' |
| 176 | + |
| 177 | +</docs-code> |
| 178 | + |
| 179 | +[CliUpdate]: cli/update "ng update | CLI |Angular" |
| 180 | + |
| 181 | +[GuideNpmPackages]: reference/configs/npm-packages "Workspace npm dependencies | Angular" |
| 182 | + |
| 183 | +[GuideWorkspaceConfig]: reference/configs/workspace-config "Angular workspace configuration | Angular" |
| 184 | + |
| 185 | +[Resources]: resources "Explore Angular Resources | Angular" |
| 186 | + |
| 187 | +[AngularMaterialMain]: https://material.angular.dev "Angular Material | Angular" |
| 188 | + |
| 189 | +[AngularUpdateMain]: https://angular.dev/update-guide "Angular Update Guide | Angular" |
| 190 | + |
| 191 | +[GetbootstrapDocs40GettingStartedIntroduction]: https://getbootstrap.com/docs/4.0/getting-started/introduction "Introduction | Bootstrap" |
| 192 | + |
| 193 | +[NpmjsMain]: https://www.npmjs.com "npm" |
| 194 | + |
| 195 | +[YarnpkgMain]: https://yarnpkg.com " Yarn" |
0 commit comments