How to install npm package from command line with tilda~? - node.js

I want to install typescript in specific version and save the version to package.json:
"typescript": "~3.9.3"
Of course I can do it by write "typescript": "~3.9.3" in package.json and then run npm i.
But I want to install by cmd and then npm will update the package.json with the version I specified.
something like:
npm i typescript#~3.9.3 --save-dev
but its not work cause in the package.json npm write:
"typescript": "^3.9.7"
I want npm to write:
"typescript": "~3.9.3"
How can I achieve that with npm?

Related

npm install -g download all the dependencies again

I have package.json file, that contains 10 dependencies,
Every time I do npm install -g,all the dependencies are downloaded again, and this takes a long time.
Is there a way to take the existing dependencies from the local node_modules directory ?
This is my package.json dependencies:
"dependencies": {
"body-parser": "~1.16.0",
"colors": "^1.1.2",
"console-stamp": "^0.2.5",
"cookie-parser": "~1.4.3",
"dateformat": "^2.0.0",
"debug": "~2.6.0",
"express": "~4.14.1",
"ip": "^1.1.5",
"jade": "~1.11.0",
"lodash": "^4.17.4",
"morgan": "~1.7.0",
"nodemon": "^1.11.0",
"promise": "^8.0.1",
"rand-token": "^0.3.0",
"random-date-generator": "^1.0.2",
"restify": "^4.3.0",
"restify-cookies": "^0.2.2",
"serve-favicon": "~2.3.2"
}
Thanks,
npm install -g installs the current package context (i.e, the current working directory) as a global package. You do not need this for installing the project dependencies. Instead you can use npm install which will install/update dependencies in node_modules folder. By default, npm install will install all modules listed as dependencies in package.json and they do not install all dependencies every time and will only update when a change has been made to package.json or the node_modules folder is cleared out.
use npm install -g option to install packages globally like cordova, gulp, yeoman etc where you need them to be available throughout multiple projects.
Conceptually, the input to npm-install is a package.json, while its output is a fully-formed node_modules tree.
This shouldn't be an issue because npm uses the cache. It returns 304 if you have already installed the module, you can check it by applying --verbose with npm install command if given it's the same version of the dependency.
What I mean is:
$ npm install express
npm http fetch GET http://registry.npmjs.org/express
npm http fetch GET 200 http://registry.npmjs.org/express
at last, it shows:
++ express#4.16.2
added 49 packages in 12.547s
If you run it again then you will see
$ npm install express
npm http fetch GET http://registry.npmjs.org/express 408ms (from cache)
npm http fetch GET 304 http://registry.npmjs.org/express 408ms (from cache)
at last, it shows:
skipping write for package.json because there were no changes
++ express#4.16.2
updated 1 package in 4.11s
It hits the cache and won't download it again.
When running npm install for a second time on a project then it will not download it again only npm will check the first level of modules to ensure they're installed.
but not traverse the dependency tree to ensure all sub-module dependencies are installed. You can run npm outdated to check if modules are missing but npm won't install them for you.
npm update express updates the express module and its dependencies. So updates are when you already have the module and wish to get the new version. the update will always update to the latest version, regardless of package.json, while npm install will respect the version given in package.json.

Npm rebuild node-sass fails

I have an old project that has a grunt as build tool. I have installed the project dependencies and grunt cli. This is the package.json file:
{
"name": "",
"version": "0.0.1",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-watch": "~0.5.3",
"grunt-sass": "^1.0.0",
"node-sass": "^3.3.3"
}
}
On trying to run grunt I got an error that I need to run npm rebuild node-sass, but I got a new error then:
npm ERR! Failed at the node-sass#3.4.2 postinstall script 'node
scripts/build.js'. npm ERR! Make sure you have the latest version of
node.js and npm installed. npm ERR! If you do, this is most likely a
problem with the node-sass package, npm ERR! not with npm itself.
I have even tried with running the command with the --force flag.
But that didn't work either, how can I fix this?

npm install with --save does not save in package.json

I have npm-5.3.0 installed on my arch Linux.
I create a new project with npm init and fill in the details. This creates a package.json file with the details.
After that when I do npm install <package> --save, the file package.json does not change, and there is no entry for dependencies in the file. I had installed the package globally previously if that matters.
I found that the --save does not work with global and I had global=true set in the .npmrc file.
Setting global=false in the file fixed it.
Check update is available
npm i -g npm-check-updates
npm-check-updates -u
npm install
you have to change
"dependencies": {
"express": "",
"mongodb": "",
"underscore": "",
"rjs": "",
"jade": "",
"async": ""
}
then run
npm update --save
ı have solved adding npm 'FIRST' module from to dependencies manuelly in package.js this problem. Next, you can try npm install moduleName --save

npm install doesn't save dependency to package.json

It does add only when I execute: npm install <package_name> --save
In the documentation though: https://docs.npmjs.com/cli/install is written this:
By default, npm install will install all modules listed as dependencies in package.json.
Which is misleading.
npm install without arguments installs all dependencies (and dev dependencies) listed in the package.json file.
npm install --production installs all the dependencies (but no dev dependency)
npm install <package> installs a package and its dependencies.
npm install <package> --save installs a package and its dependencies, and adds it in the package.json file.
Edit: Since npm 5, --save is implied.
No, it's not. I think you are misreading the sentence.
npm install without any package name (as in your quote) will install all dependencies mentioned in the package.json.
Crete package.json file in your application folder.
Exp:- var/www/html/node/rest/package.json
{
"name": "node-api",
"main": "server.js",
"version": "0.0.1",
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
"body-parser": "~1.0.1",
"mysql": "^2.5.4"
}
}
Then run npm install command your application folder
Exp:- var/www/html/node/rest npm install
You could run npm init again to update the dependencies info.

npm gives error when installing module not specified by me

I am trying to install the modules needed by my application using
npm install
When I do this, I get the following error:
npm ERR! Error: No compatible version found: gulp-util#'^3.0.0'
npm ERR! Valid install targets:
npm ERR! ["0.0.1","1.0.0","1.1.0","1.1.1","1.2.0","1.3.0","2.0.0","2.1.1","2.1.2","2.1.3","2.1.4","2.2.0","2.2.1","2.2.2","2.2.3","2.2.5","2.2.6","2.2.7","2.2.8","2.2.9","2.
2.10","2.2.11","2.2.12","2.2.13","2.2.14","2.2.15","2.2.16","2.2.17","2.2.18","2.2.19","2.2.20","3.0.0","3.0.1","3.0.2","3.0.3","3.0.4","3.0.5","3.0.6"]
However, I never require this module into my project.
I'm assuming it's an issue internal of npm or node.
Furthermore, also strange is that when I run
npm list -g
I get the following output rather than seeing the versions of npm which are installed.
├── node#0.0.0
My package.json file is:
{
"name": "myProject",
"version": "0.0.1",
"dependencies": {},
"scripts": {
"test": "gulp test"
},
"devDependencies": {
"browserify": ">=9.0.7",
"gulp": ">=3.8.11",
"gulp-concat": ">=2.6.0",
"gulp-html-replace": ">=1.5.1",
"gulp-react": ">=3.0.1",
"gulp-streamify": ">=1.0.2",
"gulp-uglify": ">=1.2.0",
"gulp-util": ">=3.0.6",
"reactify": ">=1.1.1",
"vinyl-source-stream": ">=1.1.0"
},
"engines": {
"node": ">=0.10.0"
}
}
Your version of npm is so old that it doesn't support version ranges specified using ^. You are not using that syntax but it is quite likely one of the modules you are specifying is using that syntax for it's dependencies, and so when it tries to run npm install gulp-util#'^3.0.0' it throws that error.
If you update your npm either by updating node or by installing the latest npm with npm install -g npm then you should be able to proceed.

Resources