What is the best way to update angular material in a project to latest version [email protected] ?
I tried:
npm install --save @angular/material @angular/cdk
What is the best way to update angular material in a project to latest version [email protected] ?
I tried:
npm install --save @angular/material @angular/cdk
You'll want to use the npm update command. An example would look like this.
npm update @angular/material @angular/cdk
This will install the latest stable version. If you would like to target a specific version, you would have to specify it by adding the version to the end after an @ symbol.
Additionally, you can check for outdated packages with
npm outdated
Here's the documentation on npm update.
first remove from `package-lock.json` this
// "@angular/material": {
// "version": "6.4.7",
// "resolved": "https://registry.npmjs.org/@angular/material/-/material-6.4.7.tgz",
// "integrity": "sha512-SdNx7Xovi24Kw9eU6lkLhY/7f2M7L9F+/uh6XuPr4jbGgCUVVpeeVI5ztZhsZRbj1sN+/r1p5w8u62apWWl5Ww==",
// "requires": {
// "parse5": "^5.0.0",
// "tslib": "^1.7.1"
// },
// "dependencies": {
// "parse5": {
// "version": "5.1.0",
// "resolved": "https://registry.npmjs.org/parse5/-/parse5-5.1.0.tgz",
// "integrity": "sha512-
fxNG2sQjHvlVAYmzBZS9YlDp6PTSSDwa98vkD4QgVDDCAo84z5X1t5XyJQ62ImdLXx5NdIIfihey6xpum9/gRQ==",
// "optional": true
// }
// }
// },
// "@angular/cdk": {
// "version": "7.3.7",
// "resolved": "https://registry.npmjs.org/@angular/cdk/-/cdk-7.3.7.tgz",
// "integrity": "sha512-xbXxhHHKGkVuW6K7pzPmvpJXIwpl0ykBnvA2g+/7Sgy5Pd35wCC+UtHD9RYczDM/mkygNxMQtagyCErwFnDtQA==",
// "requires": {
// "parse5": "^5.0.0",
// "tslib": "^1.7.1"
// },
// "dependencies": {
// "parse5": {
// "version": "5.1.0",
// "resolved": "https://registry.npmjs.org/parse5/-/parse5-5.1.0.tgz",
// "integrity": "sha512-fxNG2sQjHvlVAYmzBZS9YlDp6PTSSDwa98vkD4QgVDDCAo84z5X1t5XyJQ62ImdLXx5NdIIfihey6xpum9/gRQ==",
// "optional": true
// }
// }
// },
second remove from `package.json` this line
// "@angular/material": "^6.4.7",
// "@angular/cdk": "^7.3.7",
then finaly run this
npm install --save @angular/material @angular/cdk
it's better to use the angular tool to upgrade ng update by doing:
ng update @angular/material @angular/cli
in some rare cases, when you touch some material mixins this angular api doesn't work, in that case I suggest to use npm (or yarn for whom used to it) as:
# Removing the material previous version files from node_modules in the explorer or with the command
npm remove @angular/material @angular/cdk
# Or by using yarn:
yarn remove @angular/material @angular/cdk
# And re-install the new version of the packages:
npm i @angular/material@latest @angular/cdk@latest
# with yarn it gives:
yarn add @angular/material@latest @angular/cdk@latest
you should always try to use ng update @angular/material because it does not only update the packages. It also migrates your code, e.g. imports and so on.
I migrated from 7 to 11 and the imports changed from
import { MatSnackBar } from '@angular/material';
to
import { MatSnackBar } from '@angular/material/snack-bar';