node.js - how to install a single dependency globally? - node.js

My package.json file looks like this:
{
"name": "tets",
"version": "1.0.0",
"devDependencies": {
"chai": "^3.0.0",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-less": "^3.0.3",
"gulp-minify-css": "^1.2.0",
"gulp-plumber": "^1.0.1",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.2.0",
"karma": "^0.12.0",
"karma-chai": "^0.1.0",
"karma-mocha": "^0.2.0",
"karma-phantomjs-launcher": "^0.2.0",
"mocha": "^2.2.5"
}
}
To install this locally I would run npm install and to install all dependencies globally I would run npm install -g. I only want to install gulp globally, though. Is there any way to accomplish this?
As a caveat, I've integrated a gulpfile into a .NET project and have it running via a post-build event. As a pre-build event, I'm executing npm install to ensure everything is setup correctly. My hope is that any code to install only gulp globally can be added to the package.json file so I can keep my pre-build event to just npm install. Unless adding npm install gulp -g to my pre-build event is acceptable...

So #FaridNouriNeshat pointed me to a solution where gulp is accessed locally which is probably the most appropriate thing to do rather than globally install it into someone's machine. In my case, I altered my post-build event in my .NET app to point to the local install of gulp rather than a global install.
"$(SolutionDir)\Gorman.Abe.Web\node_modules\.bin\gulp" styles scripts

Related

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.

Change location for custom NPM package

I got a list of node-modules, which is managed by NPM.
This is my package.json:
{
"name": "extension-acme",
"scripts": {
"archive": "webpack -p && composer archive --format=zip"
},
"devDependencies": {
"babel-core": "^6.23.1",
"babel-loader": "^6.1.0",
"babel-plugin-transform-runtime": "^6.1.2",
"babel-preset-env": "^1.1.8",
"babel-preset-es2015": "^6.22.0",
"babel-runtime": "^5.8.0",
"vue-hot-reload-api": "^1.2.0",
"vue-html-loader": "^1.0.0",
"vue-loader": "^8.2.0",
"webpack": "^1.12.9"
}
}
This works fine. But now I'd like to include this node module: https://github.com/craigh411/vue-star-rating
Now the sources of this module have to be stored in app/components/vue-star-rating.
Is it possible to change the location for this single package?
First way
You can't install package in specific folder like app/components/vue-star-rating without node_modules folder.
You can install package like this:
npm --prefix app/components/ install <your-package>
and then installed in app/components/node_modules/<your-package>
reference:
Stackoverflow
npm folders
Second way
Also you can install package normally and move in specific folder

'npm start' throwing many 'Cannot find module' errors after cloning react-native project from linux to Mac

I have a react-native project that was started on a machine running Ubuntu. Recently I was switched to a MacBook Pro, and I cloned the project to it.
After installing the necessary dependencies (everything found in the directions here) and running npm install inside my project folder, I try to run react-native run-android and I get the error Cannot find module 'destroy'. If I run npm i -S destroy and then react-native run-android again I get another error Cannot find module 'range-parser'. Again I npm install and run the project, but this happens several more times before I can run my project. I did not have these packages in the project before, and other projects I start on a Mac do not have these. Why am I incurring these errors? I have tried deleting node_modules and running npm install several times. I have uninstalled and reinstalled node. I have deleted the project locally and tried cloning again. A fresh clone and install on old machine works perfectly.
NPM Version
- Ubuntu 5.0.4
- Mac 5.0.3
Node Version
- Ubuntu 4.2.6
- Mac 8.1.4
package.json
{
"name": "MyProject",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
},
"dependencies": {
"evil-icons": "^1.9.0",
"firebase": "^4.1.3",
"lodash": "^4.17.4",
"react": "^16.0.0-alpha.13",
"react-native": "^0.44.3",
"react-native-clean-project": "^1.0.4",
"react-native-config": "^0.4.2",
"react-native-datepicker": "^1.6.0",
"react-native-device-info": "^0.10.2",
"react-native-fcm": "^7.1.0",
"react-native-linear-gradient": "^2.0.0",
"react-native-vector-icons": "^4.2.0",
"react-navigation": "^1.0.0-beta.11",
"react-redux": "^5.0.5",
"redux": "^3.7.2",
"redux-persist": "^4.8.2",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-jest": "20.0.3",
"babel-preset-react-native": "1.9.2",
"jest": "20.0.4",
"react-test-renderer": "16.0.0-alpha.6"
},
"jest": {
"preset": "react-native"
}
}
Here is everything I must install before being able to run my project:
destroy, hosted-git-info, is-builtin-module, range-parser, validate-npm-package-license

Angular 2 UNMET PEER Dependency Error on Windows 10

I am trying to run my web application on a Windows 10 Machine with the package.json (given below). I have Node version 5.6.0 and Npm version 3.6.0. But unfortunately it's always throwing an UNMET Peer Dependency Error and the following files of the AngularLoader Hierarchy fails to load when I run my project(powered by Laravel PHP): shim.js, zone.js, reflect.js, system.js. Contrary the same config runs absolutely smooth on a Linux Dev Machine. Any sort of help for installing the same on the Windows Machine will be highly appreciated as I am quite new to the Angular 2 Domain.
I have already tried the following:
rm -rf node_modules/
npm cache clean
npm install
And also installing like npm install angular/core angular/common angular-forms
P:S: Upgrading to Angular 4 is not an option. Sorry!!!
Here is my package.json:
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-11",
"laravel-elixir-vue-2": "^0.2.0",
"laravel-elixir-webpack-official": "^1.0.2",
"lodash": "^4.16.2",
"vue": "^2.0.1",
"vue-resource": "^1.0.3"
},
"dependencies": {
"#angular/common": "^2.1.2",
"#angular/core": "^2.1.2",
"#angular/forms": "^2.1.2",
"#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.11",
"#types/jquery": "^2.0.41",
"jquery": "^3.2.1",
"ng2-ui": "^0.11.5",
"rxjs": "^5.0.0-beta.12",
"zone.js": "^0.6.26"
}
}
Here is the full install log of the Windows Machine:`
Link to Error Doc
If you use libraries that depend on Angular 4 you can expect these errors.
Take ng-bootstrap 1.0.0-alpha.25 for example; If you check the CHANGELOG.md file;
BREAKING CHANGES
ng-bootstrap requires a minimal version of Angular 4.0.3
So why does this happen? Short answer; you should get rid of the version tags ^and ~ as this is your problem to begin with:
"^1.0.0-alpha.11" allows for versions >= 1.0.0-alpha.11 < 2.0.0
use "#ng-bootstrap/ng-bootstrap": "1.0.0-alpha.11" instead.
I suggest you read up on npm semver
Some might say you do not have to drop it completely but I've had my fair share of issues. Not all developers understand semantic versioning it seems. Also, it's a very bad idea to use these wildcards with alpha and beta packages as they come with breaking changes by default it seems.
How to fix it from here?
You say you have a Linux Dev Machine where you can build this app without any issues. Try doing an npm shrinkwrap on that machine. This will write an npm-shrinkwrap.json file with all used library versions at that time. Then, when doing an npm install on the Windows 10 machine, this file will be checked to see what versions should be downloaded instead.

npm install - preserve existing files in node_modules

I have taken over a NodeJS project where, for convenience, some symlinks have been placed in the node_modules directory, which has been version controlled (just with the symlinks, not with any packages).
Upon running npm install to install the packages, the entire directory is replaced and those files are deleted, causing the script that runs after the install to fail as it depends on those files existing.
"dependencies": {},
"scripts": {
"prepublish": "deployment/prepublish"
},
"devDependencies": {
"browserify": "^9.0.8",
"bulk-require": "^0.2.1",
"bulkify": "^1.1.1",
"deasync": "^0.1.4",
"del": "^1.1.1",
"gulp": "^3.8.11",
"gulp-angular-gettext": "^2.1.0",
"gulp-compass": "^2.0.4",
"gulp-concat": "^2.5.2",
"gulp-merge": "^0.1.0",
"gulp-replace": "^0.5.3",
"gulp-rev-all": "^0.8.18",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"gulp-watch": "^4.2.4",
"jshint": "*",
"lodash": "*",
"merge-stream": "^0.1.7",
"q": "*",
"request": "*",
"run-sequence": "^1.0.2",
"stringify": "^3.1.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"vinyl-transform": "^1.0.0"
}
How do I ensure that these files are preserved upon npm install? I know for a fact this used to work in earlier versions of npm but I haven't seen anything in the changelogs.
NodeJS Version = v5.4.1
NPM Version = 3.3.12
Thanks in advance for the help!
When npm install is run, it will examine the package.json file and attempt to install any dependencies listed that are not already installed to the node_modules directory. If there are no dependencies listed, it replaces the entire node_modules directory.
To preserve the files in the node_modules directory, you can do the following:
Install a module and have it added to the dependencies list in the package.json.
Move the symlink files into the module's folder.

Resources