npm install -g download all the dependencies again - node.js

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.

Related

How to install npm package from command line with tilda~?

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?

Cant build app using npx grunt on Windows

On Windows 8.1 installed Node.js 12.16.
First I run "npm install grunt" in a root directory of project.
Installation passed succesfully, however with the following warnings:
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion#
1, but npm-shrinkwrap.json was generated for lockfileVersion#0. I'll try to do m
y best with it!
npm WARN deprecated minimatch#0.2.14: Please update to minimatch 3.0.2 or higher
to avoid a RegExp DoS issue
npm WARN deprecated coffee-script#1.3.3: CoffeeScript on NPM has moved to "coffe
escript" (no hyphen)
npm WARN deprecated minimatch#0.3.0: Please update to minimatch 3.0.2 or higher
to avoid a RegExp DoS issue
npm WARN platform-web#0.0.1 No repository field.
npm WARN platform-web#0.0.1 No license field.
npm WARN The package time-grunt is included as both a dev and production depende
ncy.
npm WARN The package object-assign is included as both a dev and production depe
ndency.
+ grunt#0.4.5
added 38 packages from 47 contributors and audited 54 packages in 3.026s
found 25 vulnerabilities (5 low, 6 moderate, 14 high)
run `npm audit fix` to fix them, or `npm audit` for details
Root directory containts grunt-folder (root_dir/grunt), in which Gruntfile.js and package.json files are located.
And this grunt-folder containts other grunt-folder (root_dir/grunt/grunt). This second grunt containts some js-files.
Then in "root_dir/grunt" I run "npx grunt". But the build aborted with the following log:
>> Local Npm module "grunt-contrib-clean" not found. Is it installed?
>> Local Npm module "grunt-contrib-copy" not found. Is it installed?
>> Local Npm module "grunt-includes" not found. Is it installed?
>> Local Npm module "grunt-text-replace" not found. Is it installed?
>> Local Npm module "grunt-contrib-concat" not found. Is it installed?
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
>> Local Npm module "grunt-contrib-cssmin" not found. Is it installed?
>> Local Npm module "grunt-file-creator" not found. Is it installed?
>> Local Npm module "grunt-war" not found. Is it installed?
>> Local Npm module "nexus-deployer" not found. Is it installed?
Warning: Task "clean:all" not found. Use --force to continue.
Aborted due to warnings.
package.json is
{
"name": "platform-web",
"version": "0.0.1",
"description": "Platform",
"devDependencies": {
"archiver": "^0.12.0",
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-cssmin": "0.12.3",
"grunt-contrib-uglify": "^0.6.0",
"grunt-file-creator": "^0.1.3",
"grunt-includes": "^0.5.1",
"grunt-text-replace": "^0.4.0",
"grunt-war": "^0.5.1",
"nexus-deployer": "^0.1.8",
"object-assign": "^4.0.1",
"time-grunt": "^1.1.0"
},
"dependencies": {
"time-grunt": "^1.0.0",
"object-assign": "^4.0.1"
}
}
What am I doing wrong?
p.s.: root-folder is:
Following is only needed for older versions of grunt. New one install the bunstubbs correctly so you can still install grunt via npm and not globally.
Install grunt-cli. This will correctly pick up whichever version of grunt the project is running and execute it.
npm install grunt-cli --save-dev
In the package.json change dev script to
"scripts": {
"start": "npx grunt-cli"
}
then run
npm run start

Sass-loader requires node-sass >=4 even if that exist

I executed a update to angular 6. And during ng serve -o I receive error that sass-loader expect node-sass.
After run ng serve -o I receive:
ERROR in ./src/sass/styles.scss (./node_modules/raw-loader!./node_modules/postcss-loader/lib??embedded!./node_modules/sass-loader/lib/loader.js??ref--14-3!./src/sass/styles.scss)
Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
at Object.sassLoader (node_modules\sass-loader\lib\loader.js:31:19)
ERROR in ./src/app/app.component.scss
Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
at Object.sassLoader (node_modules\sass-loader\lib\loader.js:31:19)
ERROR in x.component.scss
Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
at Object.sassLoader (\node_modules\sass-loader\lib\loader.js:31:19)
ERROR in x.component.scss
Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
at Object.sassLoader (loader.js:31:19)
ERROR in .x.component.scss
Module build failed: Error: `sass-loader` requires `node-sass` >=4. Please install a compatible version.
at Object.sassLoader (node_modules\sass-loader\lib\loader.js:31:19)
Obvisously I check everything (in my opinion) and I don't have idea what's going on.
Package.Json:
"devDependencies": {
"#angular-devkit/build-angular": "~0.6.0",
"#angular/cli": "6.0.0",
"#angular/compiler-cli": "6.0.0",
"#angular/language-service": "6.0.0",
"#types/jasmine": "~2.8.3",
"#types/jasminewd2": "~2.0.2",
"#types/node": "~6.0.106",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-html-detailed-reporter": "^1.1.21",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-phantomjs-launcher": "^1.0.4",
"karma-teamcity-reporter": "^1.1.0",
"phantomjs-prebuilt": "^2.1.16",
"protractor": "~5.1.2",
"node-sass": "^4.9.0",
"sass-loader": "^7.0.1",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "2.7.2"
}
dir -l node_modules says:
...
05/07/2018 08:53 AM <DIR> node-sass
...
05/07/2018 08:53 AM <DIR> sass-loader
...
I executed:
npm rebuild node-sass
and secod approach:
I removed local node-module together with %User%\AppData\Roaming\npm-cache. then I removed lock file and execuded npm
npm cache clear --force
npm install
But still no success.
What do I miss ?
npm rebuild --force
aslo helps
I fixed it with
npm install node-sass
inside the project folder, and for the project, because installing it globally (with the -g option) didn't solve the issue.
I had same issue which I fixed using following steps:
Delete package-lock.json file.
Go to node_module folder and run rm -rf node_modules.
Run npm install
The package-lock.json file will auto-update with the new dependencies version.
hope it helps.
I fixed this issue using below steps:
Open package.json
Add which version you want to install of node-sass, for example "node-sass": "^4.12.0"
Run the installation command in command line interface (CLI): npm install node-sass
The issue will get resolved.
Simply run this code...
npm install --save node-sass
To solve your issue run this command
npm install --unsafe-perm
Following steps solved this issue
Delete node-saas folder in \Users\<user_id>\AppData\Roaming\npm-cache
Delete node-saas folder in <application_folder>/node_modules
Run npm install from <application_folder>
It could probably be a version mismatch. To install the working node-sass version, you can use
npm uninstall node-sass
npm install node-sass#4.14.0
And you can choose your version number based on the following table, and the node version you have installed, which you can check by node --version
You can find full info here
This has solved my issue
You can follow these steps:
First of all Delete package-lock.json file and node_modules.
and then Run npm install
The package-lock.json file will auto-update with the new dependencies version.

How to solve npm "UNMET PEER DEPENDENCY"

I am having issues with my package.json file.
It should work fine as as I use most of the node modules in other projects, but I have this package.json below:
"dependencies": {
"#angular/common": "^2.0.0-rc.1",
"#angular/compiler": "^2.0.0-rc.1",
"#angular/core": "^2.0.0-rc.1",
"#angular/platform-browser": "^2.0.0-rc.1",
"#angular/platform-browser-dynamic": "^2.0.0-rc.1",
"#angular/router": "^2.0.0-rc.1",
"angular2-in-memory-web-api": "0.0.7",
"bootstrap": "^3.3.6",
"es6-shim": "^0.35.0",
"reflect-metadata": "^0.1.3",
"rxjs": "^5.0.0-beta.6",
"systemjs": "^0.19.27",
"zone.js": "^0.6.12"
},
"devDependencies": {
"body-parser": "^1.15.1",
"express": "^4.13.4",
"jsonwebtoken": "^6.2.0",
"mongoose": "^4.4.15"
}
and they should all run fine as all dependencies exist as angular is now in rc.4 and rxjs is on 5.0.0-beta.10.
But I get 3 unmet dependencies on
npm install
'rxjs#5.0.0-beta.10'
'rxjs#5.0.0-beta.6'
'#angular/core#2.0.0-rc.1'
I get these warnings too:
npm WARN #angular/core#2.0.0-rc.4 requires a peer of rxjs#5.0.0-beta.6 but none was installed.
npm WARN #angular/http#2.0.0-rc.1 requires a peer of rxjs#5.0.0-beta.6 but none was installed.
npm WARN #angular/http#2.0.0-rc.1 requires a peer of #angular/core#2.0.0-rc.1 but none was installed.
I have also done:
npm cache clean
npm update registry > with the registry link
npm update -g
node is on latest version and still same issue... so just wondering if there is something wrong?
I think its because the dependency resolution is a bit broken, see https://github.com/isaacs/npm/issues/1341#issuecomment-20634338
You may need to manually install top-level modules that have unmet dependencies:
npm install findup-sync#0.1.2
Or structure your package.json such that any top-level modules that are also dependencies of other modules are listed lower down.
Your problem could also be that npm failed to download the package, timed-out or whatnot. Sometimes re-running npm install remedies it.
You can also install the failed packages manually as well using npm install
Other steps that may help before attempting npm install again are:
Removing node_modules using:
rm -rf node_modules/
then
npm cache clean
To explain why removing node_modules sometimes is necessary:
Apparently if a nested module fails to install during npm install, subsequent npm install won't detect those missing nested dependencies. If that's the case, sometimes it's sufficient to remove the top-level dependency of those missing nested modules, and running npm install again.
See https://github.com/npm/npm/issues/1336

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.

Resources