Angular Libraries: Install, Type, and Update
In this article
Using libraries in Angular means installing them with a package manager, adding their types, and updating them safely—this article explains the exact commands and workflow. You'll learn how to add a library, ensure TypeScript knows its types, and update packages without breaking your app.
Background: Angular's Package Ecosystem
Angular applications are built on a modular system where most functionality comes from npm packages. The Angular CLI (Command Line Interface) manages these packages through package.json, which lists every dependency and its version range. When you create a project with ng new, the CLI generates this file along with package-lock.json, which pins the exact versions installed. Understanding this ecosystem is key because installing a library isn't just about running npm install—it also involves making sure the library's types are available and that updates don't introduce breaking changes.
Angular itself is distributed as a set of packages under the @angular/ scope, such as @angular/core, @angular/common, and @angular/forms. Third-party libraries follow a similar pattern, often using scopes like @ngrx/ or @angular/material. The Angular CLI provides a dedicated command, ng add, which not only installs a package but also runs a schematic that configures your app for the library—adding imports, styles, or other setup steps automatically.
How Installing Works: The Commands
The most common way to install a library is with npm. For a library that has Angular schematics, you should use ng add instead of npm install. Here's the difference:
npm install <package-name>– adds the package todependenciesinpackage.jsonand installs it. It does not run any Angular-specific setup.ng add <package-name>– installs the package and runs its schematic, which may update your project configuration, add modules, or install additional dependencies.
For example, to add Angular Material, you run ng add @angular/material. This command installs the package, adds the necessary styles to angular.json, and sets up animations. If you just run npm install @angular/material, you'd need to manually configure everything.
When you install a library that doesn't have schematics, you often need to import its module in your AppModule or a feature module. For instance, after installing ngx-pagination, you'd add NgxPaginationModule to your imports array.
Installing Typings: TypeScript Definitions
TypeScript needs type definitions to understand the shapes of objects from JavaScript libraries. Most modern Angular libraries are written in TypeScript and ship their own types (a .d.ts file). For those, no extra step is needed—the types are included in the package.
However, some libraries are plain JavaScript and rely on community-provided type definitions from DefinitelyTyped. To install these, you run:
npm install --save-dev @types/<library-name>For example, to get types for the popular lodash library, you'd run npm install --save-dev @types/lodash. The --save-dev flag puts the types in devDependencies because they're only needed during development, not at runtime.
If no type definitions exist for a library, you can declare a module yourself. Create a file like typings.d.ts in your src folder and add:
declare module 'some-library';This tells TypeScript to treat the module as any, which silences errors but removes type safety. A better approach is to write minimal type declarations for the parts you use.
Updating Packages: Safe Practices
Updating Angular libraries requires care because major version bumps can bring breaking changes. The Angular team follows semantic versioning: minor versions (e.g., 12.1 to 12.2) add features without breaking, while major versions (e.g., 12 to 13) may change APIs.
The safest way to update is to use the Angular CLI's built-in update command:
ng updateThis command checks your package.json for outdated packages and lists available updates. To update a specific package, run:
ng update <package-name>For example, ng update @angular/core updates the core package and its dependencies. The CLI also runs migration schematics that automatically adjust your code for breaking changes. Always run ng update from the project root and commit your changes before updating so you can revert if something goes wrong.
For third-party libraries that don't use Angular schematics, you can update with npm update <package-name>. However, this only updates within the version range specified in package.json. To update to a new major version, you must edit package.json manually or use npm install <package-name>@latest.
How to Choose the Right Library
Before installing any library, check its maintenance status. Look at the npm page for the last publish date, the number of weekly downloads, and the GitHub repository for open issues and recent commits. A library that hasn't been updated in over a year may not be compatible with the latest Angular version.
Also check the peer dependencies. A library's package.json lists which Angular versions it supports. For example, a library might have "peerDependencies": { "@angular/core": ">=12.0.0" }, meaning it works with Angular 12 and above. If you're on Angular 14, that's fine, but if you're on Angular 11, you'd need to find an older version of the library.
Finally, consider the bundle size. Some libraries are large and can bloat your application. Use tools like ng build --stats-json to analyze your bundle and see the impact of each dependency.
Common Problems and Solutions
One frequent issue is version mismatch between Angular and a library. If you see errors like Cannot find module '@angular/core' or NG6002, it's often because the library was built for a different Angular version. The solution is to update the library or downgrade to a compatible version.
Another problem is missing types. If TypeScript complains about Cannot find module 'x', you need to install the types as described above. If the error is Property 'y' does not exist on type 'z', the library's types are outdated or incorrect—check for an updated version or file an issue.
When updating, you might encounter peer dependency conflicts. npm will warn about unmet peer dependencies. For example, if library A requires @angular/common@^13 and you have version 14, you'll see an error. You can use npm install --legacy-peer-deps to bypass the check, but this is not recommended because it can lead to runtime errors. Instead, update the library or find an alternative.
Comparing ng add vs npm install
The key difference between ng add and npm install is that ng add is Angular-aware. It runs schematics that modify your project files, such as adding imports, updating angular.json, or installing additional packages. This is essential for libraries like Angular Material, which requires theme styles and animations.
For libraries without schematics, npm install is sufficient. However, you'll need to manually import the module and possibly add global styles. The Angular CLI's ng add command also checks compatibility with your current Angular version, preventing you from installing an incompatible library.
In practice, always try ng add first. If the library doesn't support it, the CLI will tell you and you can fall back to npm install.
Safety and Best Practices
Before updating any package, back up your project by committing to version control. Run ng update in a clean working tree to avoid conflicts. After updating, run your test suite and build the app to catch any issues.
Use the --dry-run flag with ng update to see what changes would be made without applying them:
ng update @angular/core --dry-runThis is a safety net that shows you the migration steps before they're executed.
Also, keep your package.json organized. Use dependencies for runtime libraries and devDependencies for build tools and types. This separation helps when you update—you can update dev dependencies more freely without affecting production.
Finally, avoid using npm audit fix automatically for Angular projects. It can update packages to versions that are incompatible with your Angular version. Instead, review each vulnerability and update manually if needed.
Frequently Asked Questions
Why do I get "Cannot find module" after installing a library?
This usually means the library is not installed or the path is wrong. Check that the package name in your import matches the one in package.json. If it's a scoped package like @angular/material, the import should be @angular/material/button for a specific entry point. Also ensure you've run npm install in the correct directory.
How do I update all Angular packages at once?
Run ng update without arguments to see all available updates. To update all Angular packages to the latest version, use ng update @angular/cli @angular/core. This will update both the CLI and the core framework packages. For a full update, you can also run ng update --all, but this may update non-Angular packages too, so review the changes first.
What is the difference between dependencies and devDependencies?
dependencies are packages needed to run the application in production, like Angular itself and UI libraries. devDependencies are only needed during development, such as TypeScript, testing tools, and type definitions. When you deploy, only dependencies are installed if you use npm install --production.
Can I use a library that doesn't have Angular schematics?
Yes. Many libraries work fine without schematics. You just need to install them with npm install and manually import the necessary modules. For example, ngx-cookie-service requires you to add CookieService to your providers. Check the library's documentation for setup instructions.
How do I check if a library is compatible with my Angular version?
Look at the library's package.json on npm or GitHub. It will list peerDependencies, which specify the Angular versions it supports. You can also run npm info <package-name> peerDependencies to see them from the command line.
What should I do if an update breaks my app?
If you've committed your code before updating, you can revert to the previous state with git checkout -- . or git revert. If you haven't committed, you can manually revert changes in package.json and run npm install to restore the old versions. Always read the migration guide for the library to understand what changed.