Only install packages that are included in the package.json - node.js

I just want to install only packages that are included in the packages.json. But when I run npm install, over 800 packages are suddenly installed. Is there a specific command to realize this or is my package.json (see below) wrong?
{
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "test",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.13.0",
"gulp": "^3.9.1",
"gulp-autoprefixer": "^5.0.0",
"gulp-concat": "^2.6.0",
"gulp-cssnano": "^2.1.2",
"gulp-htmlmin": "^4.0.0",
"gulp-imagemin": "^4.1.0",
"gulp-install": "^1.1.0",
"gulp-jade": "^1.1.0",
"gulp-jsonminify": "^1.0.0",
"gulp-ng-annotate": "^2.0.0",
"gulp-sass": "^4.0.1",
"gulp-uglify": "^3.0.0"
},
"dependencies": {
"critical": "^1.2.2",
"imagemin-pngquant": "^5.0.0"
}
}

npm install uses package.json to install packages you want AND their own dependencies. So you haven't choice unless you want broken packages ? In that case you could manually uninstall packages you don't want.

In this case when u install this packages the dependencies of that particular packages has also been installed.
Like if u install critical module then =>
bluebird
chalk
cheerio
clean-css
cli
debug
filter-css
fs-extra ... etc
has also been installed, that's the case in here

I am not sure why would that be happening, whenever we run npm install it picks up the package.json and installs packages defined in it including the transitive dependencies. By looking at your package.json there should not be 800 of them.unless you have multiple package.json linked up with each other.
I would recommend to copy package.json to a different directory and clear cached in modules and
run npm install --log-level=verbose
this should give in the details for the rest packages being called up.

Related

NPM : Error: Cannot find module 'balanced-match'

I just updated the node and npm using nvm. When I start the node server I am getting this error. I tried with different solutions. This type of questions already existed in stack overflow but none of these worked for me. So I tried with these things but no use.
1.Removed node modules and install node modules with npm install.
2.Updated babel-cli version.
3.downgraded node and nvm.
4.npm i balanced-match.
5.npm i -g balanced-match.
6.npm i balanced-match --save.
I am adding my package.json
{
"name": "******",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon express-server.js --exec babel-node",
"build": "babel ./ -d dist",
"serve": "node dist/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"bluebird": "^3.5.1",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-es6-template-engine": "^2.0.3",
"express-session": "^1.15.6",
"handlebars": "^4.0.11",
"helmet": "^3.13.0",
"html": "^1.0.0",
"instamojo-nodejs": "0.0.5",
"jsonwebtoken": "^8.2.2",
"lodash": "^4.17.10",
"moment": "^2.22.2",
"mongoose": "^5.1.3",
"mongoose-unique-validator": "^2.0.1",
"multer": "^1.3.0",
"mysql": "^2.15.0",
"node-fetch": "^2.1.2",
"node-schedule": "^1.3.0",
"node-xlsx": "^0.12.1",
"nodemailer": "^4.6.8",
"passport": "^0.4.0",
"passport-google-oauth20": "^1.0.0",
"passport-local": "^1.0.0",
"request": "^2.88.0",
"request-ip": "^2.0.2",
"sanitize-html": "^1.18.2",
"utf8": "^3.0.0",
"validator": "^10.2.0",
"xlsx2json": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"nodemon": "^1.17.5"
}
}
In console it showing like this.
console error
I wasted 12 hours time to solve this problem only.
I solved this problem by installing the balanced-match with npm install balanced-match in root folder. I don't know why it is taking root folder node modules. In console error it is clearly given path but I have not aware of it. If anyone explain why it is taking root node modules that would be helpful to me.
I did not see any node packages as balanced-match. Hence, it is giving the error. You can also manually add package in package.json and run npm install.
If you ran this command: npm i balanced-match --save there would be an entry for it in your package.json file. Does your app require this module for production or is it a development tool? That will determine which flag you should pass to the install command. From the docs:
-P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.
--no-save: Prevents saving to dependencies.
When using any of the above options to save dependencies to your
package.json, there are two additional, optional flags:
-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.
-B, --save-bundle: Saved dependencies will also be added to your bundleDependencies list.
The previous answer does have the most likely fix for your problem. You can either run the install command again with the correct flag for your use case (which will install it and update your package.json), or you can add it manually and run npm install again.
When you install, are there any errors at all? If not, it should work. You can check for installed packages using npm ls (passing optional tags like '-g' will show you all globally installed modules, and you can modify the output by also passing a depth tag like such: --depth=0.

NPM doesn't install dependencies that need build

My package.json file looks like the following :
{
"name": "anna-backend",
"version": "1.0.3",
"description": "Backend for ANNA intranet",
"main": "app.js",
"author": "IpsaOne DevTeam",
"private": true,
"license": "ISC",
"dependencies": {
"async": "^2.6.0",
"bcrypt": "^1.0.3",
"body-parser": "^1.17.2",
"mmmagic": "^0.4.6",
[...]
}
}
When I run npm install in the folder, everything installs well except the dependencies that require building via node-gyp (like bcrypt, mmmagic) and I have to install them manually by typing npm install mmmagic. Otherwise, they're just not installed and my application doesn't start.
Is it expected behaviour ? Can I do anything about it ?
mmmagic module has issues on install
https://github.com/mscdex/mmmagic/issues/111
try install at last version 0.5.0

Npm is ignoring dependencies

I decided to move my project into different folder. I didn't copy the node-modules folder.
When I run npm install, it said nothing and installed nothing. Obviously it looked into devDependencies section instead of Dependencies that I used before.
How can I make npm install all from Dependencies?
My package.json:
{
"name": "zzz",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.18.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"ejs": "~2.5.7",
"express": "~4.15.5",
"express-session": "^1.15.6",
"grunt": "^1.0.1",
"grunt-contrib-less": "^1.4.1",
"grunt-exec": "^3.0.0",
"grunt-link-html": "^0.1.2",
"moment": "^2.20.1",
"mongoose": "^5.0.0-rc1",
"morgan": "~1.9.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"serve-favicon": "~2.4.5"
}
}
If I try to install package individually it creates and updates devDependencies section, not dependencies as expected.
Solution found. It turned out that NPM stops working as expected if you have spaces in the "name" in package.json. As soon as I removed them NPM started working. It was so confusing!
There is a typo in your json in the form of a blank space near "serve-favicon": "~2.4.5". Better use an IDE like VSCODE and an extension like beautify to avoid unnecessary errors.Also its asking for a dependency "grunt#~0.4.0" . So install it and do npm install.

how to download and install all node modules through package.json?

I want to invoke and download all node modules through maven, in maven I have defined the plugins to install it but now in my package.json i have defined the following npm install command, please advise how can i install all node modules and can define them in package.json
package.json
{
"name": "MyProject",
"version": "1.0.0",
"author": "Saral",
"scripts":{
"start":"node gulpfile.js",
"prebuild": "npm install",
"build": "gulp"
},
"dependencies": {
"gulp": "*",
"gulp-ruby-sass": "*",
"gulp-util": "*",
"gulp-rename": "*",
"gulp-concat": "^2.6.0",
"gulp-concat-vendor": "0.0.4",
"map-stream": "*",
"gulp-livereload": "*",
"gulp-concat": "*",
"gulp-uglify": "*",
"gulp-minify-css" : "^1.2.1",
"gulp-notify":"2.2.0",
"gulp-inject": "1.5.0",
"run-sequence": "1.1.4",
"stream-series": "0.1.1",
"gulp-gzip": "1.2.0",
"gulp-clone": "1.0.0",
"gulp-watch": "*"
}
}
Go to your terminal and type in
npm install
This will crawl through all the required packages mentioned in package.json and download it to node_modules folder.
When using gulp in your gulpfile.js
var install = require("gulp-install");
gulp.src(['./bower.json', './package.json'])
.pipe(install());
So when you run gulp in your terminal all the packages in bower.json and package.json will be downloaded into bower_components and node_modules, respectively.
As I understand the question, just use the "Frontend Maven Plugin"
It installs node & npm: https://github.com/eirslett/frontend-maven-plugin#installing-node-and-npm
Executes npm install, which is already the default argument and can be omitted: https://github.com/eirslett/frontend-maven-plugin#running-npm

NPM not installing packages

I have a project with dependencies in package.json. When I install with npm install, I get packages installed in ~/.npm but not in the current folder's node_modules.
NPM is creating node_modules, but not installing packages there. There's a .staging folder within node_modules, which contains all of the packages for the projects, but they're not being moved out after retrieval.
Using Node v6.9.1 / npm v3.10.8.
There aren't any other projects/node_modules installed in directories higher than the current one, though there is a package.json in the parent directory as well. No node_modules directory gets created in the parent directory, however.
Strangely enough, when I manually install a package like npm install #angular/core it installs without problem.
This is on Ubuntu 16.04.
package.json:
{
"name": "",
"version": "0.5.0",
"description": "website (Angular2/NodeJS)",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "John Halbert <https://halbert.tech> (john#halbert.tech)",
"license": "SEE LICENSE IN LICENSE.md",
"dependencies": {
"#angular/common": "^2.1.1",
"#angular/compiler": "^2.1.1",
"#angular/core": "^2.1.1",
"#angular/forms": "^2.1.1",
"#angular/http": "^2.1.1",
"#angular/platform-browser": "^2.1.1",
"#angular/platform-browser-dynamic": "^2.1.1",
"#angular/router": "^3.1.1",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"systemjs": "^0.19.39",
"zone.js": "^0.6.26"
},
"devDependencies": {
"concurrently": "^3.1.0",
"typings": "^1.4.0"
}
}
Try to run
echo "" > $(npm config get userconfig)
npm config edit
To see if you have change de node modules default folder, if so, you can reset the configurations by doing
echo "" > $(npm config get globalconfig)
npm config --global edit
After doing more research it looks like this is a memory issue with my server. Running on a low-end VPS with 128Mb RAM.
There are a few suggestions on how to deal with this. I found others suggesting npm config set jobs 1 as a way to limit memory usage, but this hasn't worked for me. Others suggest adding (more) swap, which is disallowed by my hosting provider.
The npm process is actually being killed so it appears this is likely the culprit.

Resources