Peer dependency issue in npm Node modules - node.js

I have run into some issues in installing the grunt related node module dependencies for my project,
I want to install the selected dependencies using one command, so I used below,
npm install grunt grunt-contrib-jshint grunt-cli grunt-contrib-watch grunt-exec grunt-contrib-symlink grunt-contrib-clean grunt-contrib-copy grunt-text-replace grunt-git --save-dev
Got error like 'grunt-exec#0.4.6 requires a peer of grunt#~0.4 but none was installed.'
So as per the feedback from googling, found we can hardcode the dependency version like below fix,
npm install grunt#~0.4 grunt-contrib-jshint grunt-cli grunt-contrib-watch grunt-exec grunt-contrib-symlink grunt-contrib-clean grunt-contrib-copy grunt-text-replace grunt-git --save-dev
It has temporarily fixed the issue for sometime but now I get an error like
'grunt-git#1.0.0 requires a peer of grunt#~1.0.1 but none was installed.'
So it seems that the grunt plugins needs different version of same dependency - grunt(in my case). Installing the plugins one by one instead of a cluster also wont resolve the issue

Update
Since people want to take points away from this answer w/o reading the comments to see that a solution was found, here's what we arrived at:
An instance like this, you should review the previously released versions for grunt-git, find an older version that meets your reqs (v0.3.8) and double check if the features you want to use with the plugin are available or not. If it is, try npm install grunt-git#0.3.8 --save-dev. Releases can be found here.
Original Solution
First you'll want to initialize your project by creating a package.json file by running the command npm init. You'll be prompted to answer several questions, you can change the default values if you like.
Once the project has been initialized, install the dependencies one at a time & include the flag --save or --save-dev. This will add the dependency to the package.json manifest. Example: npm install grunt-contrib-jshint --save-dev
Once you have all of your dependencies listed in your package.json file, you can then achieve a one-command install using npm install.
The NPM registry has a very detailed article on using package.json if you'd like to learn more.

install grunt with the global flag
npm install -g grunt

I have fixed my issue using the suggestions from theaccordance.
Only the recent version of grunt-git(1.0.0) was having the dependency issue, so I am installing the previous version 0.3.7 which doesn't require a particular version of grunt.
npm install grunt#0.4.5 grunt-contrib-jshint grunt-cli grunt-contrib-watch grunt-exec grunt-contrib-symlink grunt-contrib-clean grunt-contrib-copy grunt-text-replace grunt-git#0.3.7 --save-dev

I encountered a similar issue today when trying to install grunt-exec.
npm ERR! peerinvalid The package grunt#1.0.1 does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer grunt-exec#0.4.6 wants grunt#~0.4
From some clues in this thread I fixed it by...
npm uninstall grunt
at the root folder of my project, and I edited package.json to remove grunt from the devDependencies.
I already had grunt installed globally, so didn't need it in the project too.
Running
npm install -D grunt-exec
a second time worked for me and my updated devDependencies now looks like this:
"devDependencies": {
"chai": "^3.5.0",
"grunt": "^0.4.5",
"grunt-contrib-clean": ">0.4.0",
"grunt-contrib-concat": "^1.0.0",
"grunt-contrib-jshint": "~1.0.0",
"grunt-contrib-uglify": "^1.0.1",
"grunt-exec": "^0.4.6",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.4.5"
},
with the new lines being grunt, and grunt-exec.
So far my project works as it did before adding grunt-exec. It looks like I just found a simple way to downgrade to an earlier version of grunt.
Here's the version info from the global install:
>grunt -V
grunt-cli v1.2.0
grunt v0.4.5
Hope this helps.

Related

npm list does not show installed package

I'm having trouble understanding npm. I have a node.js project with react, but I am not able to find the latter with npm list.
I certainly have it installed, because my project using react works.
I also have it in my package.json file under "dependencies":
"dependencies": {
"react": "^16.4.1"
}
Also, if I search for react on my drive, I find the module for it in my project folder:
user/Dev/project/node_modules/react
However, if I do npm list react, I get nothing:
me ~/D/project> npm list react
project#0.1.0 /Users/me/Dev/project
└── (empty)
Even if I do npm install or even specifically npm install react --save, no change.
Nor is the package listed with npm list or npm list -g (with or without --depth=0), except I get further indications that npm does not 'see' the react module:
UNMET PEER DEPENDENCY react#16.4.1
I'm using npm v5.6.0.
Any ideas?
I fixed this by running npm update command. (If that doesn't work try to delete node_modules and package-lock.json prior to running npm_update)

upgrade a dependency of a global package in npm

npm version: 3.10.10
node version: 6.14.2
I need to upgrade some dependencies of npm to address some security warnings. An example: sshpk is a dependency of npm via http-signature and request:
bash-4.3# npm ls sshpk -g
/usr/local/lib
`-- npm#3.10.10
`-- request#2.75.0
`-- http-signature#1.1.1
`-- sshpk#1.10.1
I need sshpk to get upgraded to >=1.14.1, which is possible given the version lock in http-signature#1.1.1's package.json:
"dependencies": {
"assert-plus": "^0.2.0",
"jsprim": "^1.2.2",
"sshpk": "^1.7.0"
},
I've tried running npm upgrade -g npm#3 and npm --depth 9999 upgrade -g npm#3 without any success. It seems that npm doesn't continue in any update action since it notices we're already on the latest npm 3.x.x release of 3.10.10. I need to be able to keep npm's dependencies up-to-date as far as security patches go. Is this possible through npm update directly? I'm thinking of something similar to yarn upgrade <package>#<version> where it will traverse a package's sub-dependencies and upgrade those, even if the parent package isn't in need of a version change.
A reliable way to reinstall the package is to remove it or node_modules and install again.
Since NPM cannot be installed without NPM, this requires Node to be reinstalled.
An alternative is to install a spare NPM wrapper (e.g. npm3) and use it to reinstall main npm package
npm i -g npm3
rm -rf npm/
npm3 i -g npm#3
--force option can be used to reinstall the package, but it doesn't guarantee that package dependencies will be reinstalled.

Node install npm modules

I'd like to install npm modules based on package.json
I'd like to know if there is anyway to automate installing modules.
For instance dependencies of package.json is as follows.
"dependencies": {
"express": "3.1.0",
"jade": "*",
"stylus": "*",
"mongodb": ">= 0.9.6-7"
}
do I have to install modules one by one like this?
npm install express#3.1.0
npm install mongodb#0.9.6
and etc.
Any help would be appreciate.
See the documentation about npm install.
By default, npm install will install all modules listed as dependencies in package.json.
So you can just type npm install.
when you are installing first time use --save, that module installation info will be added to package json
after that at new location you just need to run npm install
npm install express#3.1.0 --save
npm install mongodb#0.9.6 --save
npm install
also refer link
You can do this by typing:
sudo apt-get update
sudo apt-get install npm
use nvm to switch versions.
If you want to install a specific version of module you should use
npm install module_name#version --save
--save add's the module and the version of the module to your package.json file's dependencies. If you want to install just any version of a module you can use
npm install module_name --save
if you don't use --save at the and node would still install the last version of the module you want but it wouldn't add it to your package.json file . In this case you have some specific versions of some modules in your package.json file if you want to install them, you can simply use the
npm install
command. npm install installs all modules in your package.json file.
Also if you are new in nodeJs you can check this out. I hope this helps. Have a good day good sir.
You could install modules written in package.json as follows.
npm install

Will npm install global look in the local package.json?

Say I have "karma": "~0.12.0" in package.json, and the latest version of karma is 0.13.19. Will npm install -g karma look in the local package.json or will it install the latest version of karma available in npm?
Once you provide npm with a package name, it will not look for in the package.json file.
If you want specific version, you can provide it in the command, not in the package.json:
npm install -g karma#0.12.0

npm install vs. update - what's the difference?

What is the practical difference between npm install and npm update? When should I use which?
The difference between npm install and npm update handling of package versions specified in package.json:
{
"name": "my-project",
"version": "1.0", // install update
"dependencies": { // ------------------
"already-installed-versionless-module": "*", // ignores "1.0" -> "1.1"
"already-installed-semver-module": "^1.4.3" // ignores "1.4.3" -> "1.5.2"
"already-installed-versioned-module": "3.4.1" // ignores ignores
"not-yet-installed-versionless-module": "*", // installs installs
"not-yet-installed-semver-module": "^4.2.1" // installs installs
"not-yet-installed-versioned-module": "2.7.8" // installs installs
}
}
Summary: The only big difference is that an already installed module with fuzzy versioning ...
gets ignored by npm install
gets updated by npm update
Additionally: install and update by default handle devDependencies differently
npm install will install/update devDependencies unless --production flag is added
npm update will ignore devDependencies unless --dev flag is added
Why use npm install at all?
Because npm install does more when you look besides handling your dependencies in package.json.
As you can see in npm install you can ...
manually install node-modules
set them as global (which puts them in the shell's PATH) using npm install -g <name>
install certain versions described by git tags
install from a git url
force a reinstall with --force
npm install installs all modules that are listed on package.json file and their dependencies.
npm update updates all packages in the node_modules directory and their dependencies.
npm install express installs only the express module and its dependencies.
npm update express updates express module (starting with npm#2.x, it doesn't update its dependencies).
So updates are for when you already have the module and wish to get the new version.
In most cases, this will install the latest version of the module published on npm.
npm install express --save
or better to upgrade module to latest version use:
npm install express#latest --save --force
--save: Package will appear in your dependencies.
More info: npm-install
npm update: install and update with latest node modules which are in package.json
npm install: install node modules which are defined in package.json(without update)
Many distinctions have already been mentioned. Here is one more:
Running npm install at the top of your source directory will run various scripts: prepublish, preinstall, install, postinstall. Depending on what these scripts do, a npm install may do considerably more work than just installing dependencies.
I've just had a use case where prepublish would call make and the Makefile was designed to fetch dependencies if the package.json got updated. Calling npm install from within the Makefile would have lead to an infinite recursion, while calling npm update worked just fine, installing all dependencies so that the build could proceed even if make was called directly.

Resources