Update npm packages ignoring symbols like ^ and ~ - node.js

I have the following dependencies in my project:
"dependencies": {
"discord.io": "^2.5.3",
"discord.js": "^12.5.3",
"ms": "^2.1.3",
"node-localstorage": "^2.2.1",
"winston": "^3.3.3",
"xmlhttprequest": "^1.8.0"
}
I wanted to upgrade these to the latest version, but if I use npm update the symbol ^ prevents for example the package discord.js to update to the version 13.1.0. I want to ignore that symbol and force it to upgrade to the latest version.
I solved this problem by removing the package from the package.json, running npm i and then reinstalling the dependency. Is there any other possibility to do this automatically for all dependencies?

Related

Using yarn, how to update package.json with actual version installed of packages?

I'm working on a project where dependencies version are not strictly fixed (using the ^), like so:
...
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
...
Now, I'd like to strictly fix all version (remove the ^) but I'd like to keep the version actually installed (and saved in yarn.lock) at the moment and write it to package.json.
For example, for react: ^16.13.1, installed version is 16.14.0.
So I'd like my package.json to be:
...
"dependencies": {
"react": "16.14.0",
"react-dom": "16.14.0"
}
...
Is there a command to do this automatically?
The solution came from npm-check-updates.
Running this command, it updates my package.json with latest minor version for all packages:
ncu -t minor -u
Then, a little yarn install to actually install these packages and you can safely remove all the ^ of your package.json.

How "npm update" command works?

I got 3 dependencies on my project, when I run npm update it doesn't update the version of one dependency (axios).
Here is my package.json file:
{
...
"dependencies": {
"axios": "^0.25.0",
"date-fns": "^2.26.0",
"lodash": "^4.17.15"
}
}
When I enter the npm update or npm update --save command, this is my actual result:
{
...
"dependencies": {
"axios": "^0.25.0",
"date-fns": "^2.29.1",
"lodash": "^4.17.21"
}
}
As I can see, the axios package doesn't get updated.
My expected result is to get the axios package at version 0.27.2 (latest at this day).
From docs
Caret Ranges ^1.2.3 ^0.2.5 ^0.0.4
Allows changes that do not modify the left-most non-zero element in the [major, minor, patch] tuple.
Since your axios dependency is defined as ^0.25.0, with its minor version 25 being the left-most non-zero element, it will allow update to 0.25.X, but not to 0.27.0.
Either change the dependency in your package.json, or let npm do it. This should install the newest version:
npm install axios#* --save
You can use npm update -d or npm update --save-dev to update developer dependencies. (Dependencies used in development) Using npm update only would ignore the dev dependencies.
You can now use the command npm install axios and it will automatically update axios in your package.json

How do I identify which npm packages are just peer dependencies?

I'm trying to remove unused packages from the package.json files for a few projects but I'm running into issues with peer dependencies. There are some tools, such as depcheck, which try to list all of the "unused" packages, but it doesn't differentiate between actual unused packages, and packages that are unused because they're peer dependencies.
Is there a package out there, or some npm command I'm not familiar with, that will allow me to either list all peer dependencies in my project or, at the very least, allow me to type in a package name and see if that package is installed because it's a peer dependency of another package?
For posterity, here's an example of just the dependencies for one of my projects. In this project, I know for instance that reflect-metadata is a peer dependency of #nestjs/common, but I only discovered that after uninstalling it.
"dependencies": {
"#google-cloud/storage": "^3.2.1",
"#google-cloud/vision": "^1.3.0",
"#google/maps": "^0.5.5",
"#nestjs/common": "^6.6.7",
"#nestjs/core": "^6.6.7",
"#nestjs/platform-express": "^6.6.7",
"#slack/webhook": "^5.0.1",
"#typeform/api-client": "^1.5.1",
"algoliasearch": "^3.34.0",
"array-uniq": "^2.1.0",
"basic-auth": "^2.0.1",
"child-process-promise": "^2.2.1",
"class-transformer": "^0.2.3",
"class-validator": "^0.10.0",
"express": "^4.17.1",
"firebase-admin": "^8.5.0",
"firebase-functions": "^3.2.0",
"geoip-lite": "^1.3.8",
"geolib": "^3.0.4",
"glob": "^7.1.4",
"hbs": "^4.0.4",
"hubspot-api": "^2.2.10",
"json2csv": "^4.5.3",
"lodash": "^4.17.15",
"luxon": "^1.17.2",
"node-fetch": "^2.6.0",
"postmark": "^2.2.9",
"promise-settle": "^0.3.0",
"qrcode": "^1.4.1",
"redux": "^4.0.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.3",
"sales-tax": "^2.0.10",
"sanitize-filename": "^1.6.3",
"sharp": "^0.23.0",
"stripe": "^7.9.0"
},
This is a great question, not sure why it was downvoted.
Unfortunately I don't know of an existing, nicely automated way to do this.
You can test an individual package like so:
npm uninstall some-package && npm ls
If there are any peer dependency violations, they will be printed out and the command will exit nonzero.
So you could combine this with the output of one of the other tools mentioned, iterate through the candidates for orphaned packages, remove them one-by-one, and test the output between each change. Then do an npm uninstall --save to commit the ones that didn't produce an error, or npm install to roll back the ones that do. This could be automated, but I will leave that as an exercise to the reader.
check-peer-deps
Verifies that the peerDependency requirements of all top level dependencies are satisfied.
Installation
You can install this on your system with:
npm i -g check-peer-deps
Please note that this utility requires npm to be available.
Usage
Simply change into the directory of the project you wish to check the peerDependencies of and run the program.
 cd foobar
 check-peer-deps
If the minimum versions of all your top level peerDependencies are satisfied then there will be no output, otherwise you will see something similar to this:
check-peer-deps A dependency satisfying eslint-config-airbnb-base's peerDependency of 'eslint#^4.9.0' was not found! Current: eslint#^4.6.0 Package dependencies can satisfy the peerDependency? Yes
This tells you that eslint-config-airbnb-base is requiring eslint#^4.9.0 as a peerDependency, but the project currently only specifies eslint#^4.6.0, allowing a potential issue to arise if eslint#4.6.0 was installed and not updated before installing. The output also tells you that although the minimum allowed version is too low, the maximum allowed version does satisfy the peerDependencies requirement.
install-peers-cli
CLI to install project's peerDependencies, without side effects. Works with npm, yarn. Supports yarn workspaces flow.
Install
yarn
$ yarn add --dev install-peers-cli
npm
$ npm install --save-dev install-peers-cli
Usage
Add package.json script:
{
"scripts": {
"install-peers": "install-peers"
}
}
Then run yarn install-peers (or npm run install-peers) to install peer dependencies of your project. It won't update lock files or modify package.json, keeping your setup pure and clean. Any other lifecycle script could be used depending on your use case.
You still may see "unmet peer dependency" warnings during regular install phase, due to installation flow of npm/yarn.
There will be a file called package-lock.json after once you do npm install.
By analyzing package-lock.json file, you can understand the dependencies of each package.
For more details this blog can be referred.
Dependencies of a package are required for the correct running of the package.
But there are some optional dependencies, which can be skipped.
You can use --no-optional argument while installing so these extra packages will not be installed.
But make sure your application is working fine without these optional packages.

Could not find the implementation for builder #angular-devkit/build-angular:dev-server on ng serve command [duplicate]

This question already has answers here:
Angular 6 - Could not find module "#angular-devkit/build-angular"
(47 answers)
Closed 8 months ago.
I tried to update the angular CLI following this, but now I can't run my app. When I try to run the command ng serve, it gives me this error:
Could not find the implementation for builder #angular-devkit/build-angular:dev-server
Error: Could not find the implementation for builder #angular-devkit/build-angular:dev-server
at WorkspaceNodeModulesArchitectHost.resolveBuilder (C:\Users\Lupus\Documents\full-stack-projects\financial-app-ui\node_modules\#angular\cli\node_modules\#angular-devkit\architect\node\node-modules-architect-host.js:49:19)
at ServeCommand.initialize (C:\Users\Lupus\Documents\full-stack-projects\financial-app-ui\node_modules\#angular\cli\models\architect-command.js:135:55)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
I have tried everything I could found.
I tried to manually install devkit with the command npm install --save-dev #angular-devkit/build-angular
I tried to run the command npm i --only=dev
I tried to run the commands
:
npm install
ng update
npm update
Removed the node_modules folder and the package-lock.json file and ran the commands all again.
Nothing worked. Running ng v gives me this:
Angular CLI: 8.0.3
Node: 10.15.0
OS: win32 x64
Angular: 8.0.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
#angular-devkit/architect 0.13.9
#angular-devkit/build-angular 0.13.9
#angular-devkit/build-optimizer 0.13.9
#angular-devkit/build-webpack 0.13.9
#angular-devkit/core 7.3.9
#angular-devkit/schematics 8.0.3
#angular/cli 8.0.3
#ngtools/webpack 7.3.9
#schematics/angular 8.0.3
#schematics/update 0.800.3
rxjs 6.5.2
typescript 3.4.5
webpack 4.29.0
And my package.json is like this:
{
"name": "financial-app-ui",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"#angular/animations": "^8.0.1",
"#angular/cdk": "^8.0.1",
"#angular/common": "^8.0.1",
"#angular/compiler": "^8.0.1",
"#angular/core": "^8.0.1",
"#angular/forms": "^8.0.1",
"#angular/platform-browser": "^8.0.1",
"#angular/platform-browser-dynamic": "^8.0.1",
"#angular/router": "^8.0.1",
"core-js": "^2.6.9",
"font-awesome": "^4.7.0",
"primeicons": "^1.0.0",
"primeng": "^7.1.3",
"rxjs": "~6.5.2",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"#angular-devkit/build-angular": "^0.13.9",
"#angular/cli": "^8.0.3",
"#angular/compiler-cli": "^8.0.1",
"#angular/language-service": "^8.0.1",
"#types/jasmine": "~2.8.8",
"#types/jasminewd2": "~2.0.3",
"#types/node": "~8.9.4",
"codelyzer": "~4.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.4.5"
}
}
Does anyone have any other suggestion?
I got this working by reinstalling the build-angular package. Note the '--save-dev' flag on the install command:
npm uninstall #angular-devkit/build-angular
npm install --save-dev #angular-devkit/build-angular
Resolved my issue by using the below command:
ng update #angular/cli #angular/core --allow-dirty --force
I had the same problem. Just two command solved my problem
npm uninstall #angular-devkit/build-angular
npm install --save-dev #angular-devkit/build-angular
//OR
npm install #angular-devkit/build-angular
If this doesn't work. don't worry. run this command.
ng update #angular/cli #angular/core --allow-dirty --force
I was able to solve this problem by installing the package #angular-devkit/build-angular using this command:
npm install #angular-devkit/build-angular --force
ng update should have updated the version for your #angular-devkit/build-angular
I ran into this as well and what I did was created a new Angular app and ensured that ng serve was working fine for that newly created Angular app. I then copied and used the following from the working Angular app's package.json, which is:
`"#angular-devkit/build-angular": "~0.802.2",`
Ensure you run npm install
(The version would most probably change with each Angular update - so check what version you need to use by following the steps I have outlined - if ng update doesn't already update it for you)
Make sure you have this configuration in your angular.json
"serve": {
"builder": "#angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "angular8:build"
},
"configurations": {
"production": {
"browserTarget": "angular8:build:production"
}
}
}
And make sure you have installed this package
#angular-devkit/build-angular
I'm having the same issue, but in my case I am trying to serve a downloaded template of an Angular project. At first it gave me this error after a ng serve --open terminal command.
An unhandled exception occurred:
Could not find module "#angular-devkit/build-angular" from "D:\WORK\Desarrollo\plantillas descargadas\argon-dashboard-angular-master".
I solved that following this Stack Overflow question: https://stackoverflow.com/a/50333128,
but got these warnings:
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN #ngtools/webpack#7.3.1 requires a peer of #angular/compiler-cli#>=5.0.0
<8.0.0 || ^7.0.0-beta.0 but none is installed. You must install peer dependencies
yourself.
npm WARN ajv-keywords#3.4.1 requires a peer of ajv#^6.9.1 but none is installed. You
must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.2.9
(node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for
fsevents#1.2.9: wanted {"os":"darwin","arch":"any"} (current:
{"os":"win32","arch":"x64"})
But then I got this error after trying the command:
ng serve --open
An unhandled exception occurred: Could not find the implementation for builder
#angular-devkit/build-angular:dev-server
I noticed that there were some files missing in my node_modules folder, probably caused by my current connection speed. Anyway I tried on a different computer, somewhere else, opened a clone of the project itself and ran the command:
npm update
then take the project in a .rar file to my working computer and decompressed it, run my VS Code and it just worked perfectly.
In angular.json change this line "builder": "#angular-builders/build-angular:browser", to "builder": "#angular-devkit/build-angular:browser", solved my problem.
Since you got this error, you might be using an app which uses old version of angular dev such as 0.6.x
If you simply uninstall and reinstall , it's very likely that you got other errors like "cannot find module '#angular/compiler-cli/src/tooling'" or Cannot find module '#angular/compiler-cli/ngcc' because it seems that angular has been making a lot changes.
If you solved the problem simply by updating , then forget about the following.
It's best to use the next version(relative to 0.6.5) that doesn't cause the build error and ngcc error.
In my case, the correct version is between the build error and ngcc error.
This worked for me.
First run update command ie. ng update
On running this you may get a list of other packages that should also be updated.
Install these packages manually as suggested.
After this on running ng serve I got error in angular.json for missing tsconfig.app.json file. These I added manually from this link . I also added tsconfig.spec.json file.
That is it, I ran ng serve again and the project complied successfully.
If you have npm 7+ installed in your machine try this command in terminal:
npm install --legacy-peer-deps instead of npm install and then run ng serve.
I faced this same issue while running the projects build on older version but none of the solution above could help me. Finally, I found the solution and I am sharing it with you.
Double Check Versions:
I looked at package.json & checked the all versions of all the Angular/Webkit programs, removing the ones giving me errors Typescript errors such as rxjs and googlemaps.
Remove the package-lock.json. Add all the correct versions of Angular, Webkit, Typescript, etc... checking to see what past versions render. This means looking through the Version history for packages on npmjs.com and matching by date / age and version numbers. Make the changes in package.json and save it.
"dependencies" {
"#agm/core": "1.0.0",
"#angular/cdk": "6.1.0",
"rxjs": "6.0.0",
"rxjs-compat": "6.0.0",
...
},
...
"devDependencies": {
"#angular-devkit/build-angular": "0.6.1", // old: "^6.1.0",
"#angular/cli": "6.1.0", // new: "6.1.0",
"#angular/compiler-cli": "6.1.0",
"typescript": "2.7.2"
...
}
Start fresh by removing all the old modules:
rm -fR ./node_modules/#angular; rm -fR ./node_modules/#angular-devkit/; rm -fR ./node_modules/typescript; rm -fR ./node_modules/rxjs*; rm package-lock.json;
Then re-install:
npm install;
.Pug Template Fix: I'm using a .pug loader so I had to manually patch: node_modules/#angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js to allow Webpack to render Pug files. Add this line after the html rule inside that file:
{ test: /.pug$/, use: [ { loader: "apply-loader" }, { loader: "pug-loader" } ] },.
This is imitating what the ng-add-pug-loader.js file is doing to load .pug files in Angular version 6.

NPM CLI install/update single git dependency

I have a package.json with a lot of dependencies. It can look something like that (I omitted the other keys):
{
"dependencies": {
"body-parser": "git+ssh://git#github.com:arik-so/someRepository.git",
"bs58check": "1.0.5",
"canonical-json": "0.0.4",
"compression": "1.4.0",
"cookie-parser": "1.3.1",
"cookie-session": "^1.1.0",
"csv": "0.4.2",
"zpad": "0.5.0"
}
}
Now, what I am trying to do is the following: I would like to use the CLI to only update the body-parser dependency. I am fine with reinstalling it, doing whatever needs to be done. I just do not want to have to do the full package installation because it takes too long. How would I go about doing that?
npm install --save 'git+ssh://git#github.com:arik-so/someRepository.git#master' will install the latest version of this package

Resources