Versions of npm packages - node.js

I installed Express and the package.json shows me "express": "^4.14.0". But when i do npm express -v via console inside my project, it gives me 3.8.8. Can somebody explain, where the different versions come from and how I can fix this?

inside your project run npm install and
then run npm list express to see the version inside your project.
run npm -g list express to check if it may have been installed globally.

Related

Can't run Angular application with ng serve

enter image description here
Hi.
I can't build and run my Angular application. I tried to reinstall angular/cli, node.js and remove all node modules packages globally but it did not help.
try to do:
npm install
if the error persist you can do
npm install jquery --save
If the error still happens after npm install, try deleting the whole node_modules folder and run npm install again.

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)

NPM Install is not installing dependencies

I'm attempting to install the Ushahidi V3 Client. I've been following the install process up until when I need to build the project from the source repo using npm and gulp - both of which I've had zero experience with. Whenever I run sudo npm install in the project directory, the process runs without complaints. However, when I run npm ls to verify that dependencies have been downloaded, I get a bunch of dependencies listed out as being missing.
How do I get npm to resolve all of these dependencies?
System Details
OS Ubuntu 14.04 (Trusty)
Node JS v0.12.9
NPM v3.5.1
What I've tried
Removing node_modules folder and re-running sudo npm install as referenced in this SO answer for a similar question: npm Gulp dependencies missing, even after running npm install
Uninstalling and reinstalling node and npm
#Strainy, as your research :D
It was a combination of running as sudo and not having the build-essentials.
That's why you should not use sudo npm
Follow these steps:
try npm uninstall. and then try npm install.
Also If it still doesn't work.
Try:
npm install -g npm-install-missing
or
npm-install-missing
For further reading, click here.

I require Express 3.x for my project but after installing I keep getting ' express --version 1.4.28'

I have tried many things. Uninstalling npm and express.
Following this thread: Node.js npm install express error fetch failed
Can someone explain what I am not doing Incorrectly.
I know this is simple and I have resolved this in other circumstances but this will not resolve.
package.json
"dependencies": {
"bcrypt-nodejs": "~0.0.3",
"consolidate": "~0.9.1",
"express": ">=3.2.6",
"mongodb": "~1.4.34",
etc.
At prompt if I do 'npm install' I thought it would use the version listed in the dependency list but it installed express 4.0. see below.
> npm install
Shouldn't that install the versions in the dependency list ?
Express 4.0; it is not compatible with my course. 4.x is the default at this point in time.
>npm install express
npm express --version
4.0
>rm -rf express
>which express
(returns nothing)
>npm install express#3.2.6
>npm express --version
1.4.28
I think 1.4.28 is the version of npm itself.
What is going on please ?
You're correct, you're looking at the npm version. To see the version of your installed packages use
npm ls
or for express only
npm ls express

How to use a specific version of NPM?

How can I switch which version of npm I'm using?
Currently:
$ npm -v
1.1.65
But I need: 1.0.x
I tried but got an error:
$npm version 1.0
npm ERR! version No package.json found
Anyone know how to use a different version of NPM? Thanks
Your NPM version is tied to your NodeJS version. As far as I can tell you can only have one NPM version per Node version. Using something like nodenv or, my favorite, asdf, you can define your node version per folder.
Per Node version (e.g. per folder) you globally install the version of NPM that you want to use.
$ npm install -g npm#x.x
Edit: You can also now specify NPM version in your package.json "engines" key, though it takes a little more work to enforce that declaration.
You can update npm without installing another version of node.js and npm is not tied to versions of node.js specifically.
Of course, as node.js advances and adds features so too does npm so there is some limit for npm depending on node.js but you SHOULD update npm as much as you can because there are important security patches and bug fixes.
This is a best practice for all software. Stability is not worth ignoring a security risk.
The command is npm install npm#latest -g to install it globally. This will install the latest version that will run with the node.js you have installed.
Additionally you can install a specific version of npm to your package.json in a project like this npm install npm#6.14 and you can use it locally. What is even more interesting is you can install a local version of node.js in a project and use it too!
Example:
I am running node 16.8.0 and I have npm 7.21.0 but I want to use an earlier version of npm but just for a project:
In the project directory
npm i --save-dev npm#6.14
This will put the earlier version of npm into the node_modules/.bin which will let you run it in relation to this project. If you type npm -v at this point you will get your global version.
If you type node_modules/.bin/npm -v you will get 6.14.15
Since you have made this version of npm the one for this project, when you use npm in your scripts it will use 6.14.15 and not the global version.
The same is true of node.js.
You can install an older version of node.js like this: node_modules/.bin/npm install --save-dev node#lts which will then add this version of node.js to the project.
You can test this by entering node_modules/.bin/node -v and in my example you will see the locally installed version number, in my example v14.17.3 although my global is v16.8.0
If you do this, your project scripts in package.json scripts will run the locally installed versions rather than the global versions.
You can test this out by creating a script in your package.json.scripts like this: "what:version": "npm -v && node -v",
Then if you run npm run what:version you will get
> what:version
> npm -v && node -v
6.14.15
v14.17.3
What value is this?
It is a way for you to package node and npm with your project. This can be especially valuable for large projects where you have many people working on the same project, so you have consistency across the board or if you have a build server that has a specific version of node + npm. Additionally if you have corporate requirements, etc.
Additionally there is npx which allows you to do so much more. Using npx you can even try something out without installing it.
Here is the documentation for npm: https://docs.npmjs.com/about-npm-versions
If you find you are needing to switch between different versions of node.js and npm you might want to use nvm https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
When you install npm you only get the latest package, so you need to install specific versions individually, e.g:
npm install -g npm#5.6.0
Once you've done that, run a version check and you should see the version you've just installed:
npm -v
5.6.0
You can install specific version by bellow command.
npm install -g npm#6.14.11
For a specific version, use
npm install -g npm#x.y.z
And for the latest version, use
npm install -g npm#latest

Resources