Nodes_modules packages duplicated - node.js

Im using npm to install several packages for my application.
So I got one node_modules that contains all the packages. However several packages contains itself some node_modules and these node_modules contains other packages...
My problem is that the packages may be duplicated in the several node_modules, so I m searching a solution to remove this duplications.
This what I have tried :
npm dedupe : it doesnt resolve my problem and the duplication still present
flatten-packages : this resolved the duplciations but my app stopped to work because it says that the packages are not finding the dependencies it needs....
I m using npm 6.1.0
here are an example of my dependencies and devDependencies
"dependencies": {
"axios": "^0.17.0",
"cpx": "^1.5.0",
"lodash": "^4.17.5",
"node-sass-chokidar": "0.0.3",
"npm-run-all": "4.1.1",
"object-mapper": "^5.0.0",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-intl": "^2.4.0",
"react-number-format": "^3.3.1",
"react-promise": "^2.0.1",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
},
"devDependencies": {
"eslint": "^4.18.2",
"eslint-plugin-react": "^7.7.0",
"jest": "^22.4.4"
}
Any ideas ?

There can be different versions of same package.
if dependency 'A' needs dependency 'B' with version specified as '^1.0.0'
and another dependency 'C' needs dependency 'B' but with version specified as '^2.0.0'
Package managers can put a less used version inside node_modules of another package.
It is by design.

Related

Cannot find module '#nestjs/core' or its corresponding type declarations

Couple of weeks ago I installed NestJS globally on my computer.
Now I'm coming back to that, to start learning.
So I created new project with comand:
nest new ./
It generated all the files, but when I try to run this application with command:
nest start
I expected this template project to start but there are errors with importing paths?
Is this due to version of my installed Nest or something?
Errors on the screen:
Version of my Nest:
7.5.6
Package.json:
"dependencies": {
"#nestjs/common": "^7.6.13",
"#nestjs/core": "^7.6.13",
"#nestjs/platform-express": "^7.6.13",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.6"
},
"devDependencies": {
"#nestjs/cli": "^7.5.6",
"#nestjs/schematics": "^7.2.7",
"#nestjs/testing": "^7.6.13",
"#types/express": "^4.17.11",
"#types/jest": "^26.0.20",
"#types/node": "^14.14.31",
"#types/supertest": "^2.0.10",
"#typescript-eslint/eslint-plugin": "^4.15.2",
"#typescript-eslint/parser": "^4.15.2",
"eslint": "^7.20.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.1.3",
"ts-jest": "^26.5.2",
"ts-loader": "^8.0.17",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.5"
},
I had the same issue. The following two commands fixed it:
npm install #nestjs/common
npm install #nestjs/core
These commands make sure you are using the lastest versions of those packages.
Run
npm i --save #nestjs/config
Reference: https://docs.nestjs.com/techniques/configuration
Run this command in terminal "npm install --save #nestjs/config"
I've stuck with this problem when I built a nodejs application (like, express, nestjs, etc.)
So when you build a nodejs app, the build(dist) folder just contains your code, not node_modules.
You can copy node modules to a build folder, like:
cp package.json build/package.json && cd build && npm install --only=production
Or try other solutions

I'm getting this error on build using tsc

I'm getting this error on the build using tsc
Can someone check this, please?
node_modules/#types/node/index.d.ts:75:11 - error TS2300: Duplicate identifier 'IteratorResult'.
75 interface IteratorResult<T> { }
I'm using latest typescript and #types/node
My package json configuration
"dependencies": {
"bcrypt": "^3.0.2",
"bluebird": "^3.5.1",
"body-parser": "^1.18.2",
"dotenv": "^6.2.0",
"express": "^4.16.2",
"express-statsd": "^0.3.0",
"express-validator": "^5.3.0",
"hashids": "^1.2.2",
"jsonwebtoken": "^8.4.0",
"mailgun-js": "^0.22.0",
"mysql2": "^1.5.1",
"nyc": "^13.1.0",
"reflect-metadata": "^0.1.10",
"sequelize": "^5.21.7",
"sequelize-typescript": "^1.1.0",
"strong-error-handler": "^2.3.0",
"supertest": "^3.3.0",
"uuid": "^3.3.2"
},
"devDependencies": {
"#types/express": "^4.0.39",
"#types/mocha": "2.2.41",
"#types/node": "12.12.31",
"mocha": "^5.2.0",
"ts-node": "^8.4.1",
"tslint": "^5.8.0",
"typescript": "^3.8.3"
}
someone can help me, please?
Any update to do?
I would suggest you to reinstall your packages. This should solve your problem.
sudo rm -rf node_modules/ package-lock.json
sudo npm cache clean --force
sudo npm install -D #types/node
sudo npm i --unsafe-perm
Ps: You may get some type errors after all these fresh installs. This is something you will have to fix by declaring or correcting or installing "#types/". You have reinstalled your modules and some type errors appeared.Find the the code where the types are not compatible and fix it. This way you will understand reading typescript errors and fixing types. And your code will be type safe, whole point of using Typescript.
Did you try npm install package.json (if you are not using visual code terminal you might need to add the correct path to your file!)

node npm install to central location

I was wondering if it is possible to do a single installation of node packages that can then be accessed by different development applications. For example, whenever I'm working on a react app, I do npm install to get all node packages the app needs. However, it seems that this gets a lot of unneeded extras. Is there a way
to just get what my app needs as specified in the package file or
get everything into the node modules folder in the installation folder and access the packages from there without having to reinstall them locally in every app folder.
I'm adding the following since my question is not being answered:
Let's say I have the following package.json. If I navigate to my app folder where this package.json file is located and I do npm install, what gets installed and where? My assumption is that only the packages listed in devDependencies and in dependencies will be installed and that it will be installed locally since I did not specify global. So why do I see hundreds of other packages (packages not in the list below) when I go into my node_modules folder in my app?
what I would like is to install only the packages listed below and to make them global so that if I go into another app directory I do not have to reinstall these.
{
"devDependencies": {
"babel-eslint": "7.1.1",
"babel-jest": "18.0.0",
"concurrently": "3.1.0",
"enzyme": "2.7.1",
"eslint": "3.12.1",
"eslint-config-defaults": "9.0.0",
"eslint-plugin-react": "6.7.1",
"jest": "18.1.0",
"json-server": "0.9.4",
"react-addons-test-utils": "15.4.2",
"react-test-renderer": "15.4.2",
"rimraf": "2.5.4",
"sass-lint": "1.10.2",
"tslint": "4.3.1",
"webpack-dev-server": "2.6.1"
},
"dependencies": {
"bootstrap": "4.0.0-alpha.6",
"bootstrap-loader": "2.0.0-beta.20",
"copy-webpack-plugin": "4.0.1",
"css-loader": "0.26.1",
"empty": "0.10.1",
"extract-text-webpack-plugin": "2.0.0-rc.3",
"file-loader": "0.10.0",
"html-loader": "0.4.3",
"html-webpack-plugin": "2.26.0",
"key-mirror": "1.0.1",
"lodash": "4.16.2",
"node-sass": "4.5.0",
"null-loader": "0.1.1",
"postcss": "5.2.9",
"postcss-import": "9.1.0",
"postcss-loader": "1.2.1",
"raw-loader": "0.5.1",
"react": "15.4.2",
"react-addons-css-transition-group": "15.4.2",
"react-dom": "15.4.2",
"react-redux": "5.0.2",
"react-router": "3.0.0",
"react-router-redux": "4.0.7",
"redux": "3.6.0",
"redux-thunk": "2.1.0",
"resolve-url-loader": "1.6.1",
"sass-loader": "5.0.1",
"style-loader": "0.13.1",
"ts-loader": "1.3.3",
"typescript": "2.1.4",
"url-loader": "0.5.7",
"webpack": "2.2.1",
"whatwg-fetch": "2.0.3"
},
}
This is exactly what global packages are for. npm i -g <package> also i recommend using nvm to manage your different node versions. This way you can have different global packages depending on version. You can even set aliases for multiple installs.
You can globally install nodeJs and then do npm install for the packages you need and save it in package.json. This way you would know which packages to install next time and just do npm install.

npm install - preserve existing files in node_modules

I have taken over a NodeJS project where, for convenience, some symlinks have been placed in the node_modules directory, which has been version controlled (just with the symlinks, not with any packages).
Upon running npm install to install the packages, the entire directory is replaced and those files are deleted, causing the script that runs after the install to fail as it depends on those files existing.
"dependencies": {},
"scripts": {
"prepublish": "deployment/prepublish"
},
"devDependencies": {
"browserify": "^9.0.8",
"bulk-require": "^0.2.1",
"bulkify": "^1.1.1",
"deasync": "^0.1.4",
"del": "^1.1.1",
"gulp": "^3.8.11",
"gulp-angular-gettext": "^2.1.0",
"gulp-compass": "^2.0.4",
"gulp-concat": "^2.5.2",
"gulp-merge": "^0.1.0",
"gulp-replace": "^0.5.3",
"gulp-rev-all": "^0.8.18",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"gulp-watch": "^4.2.4",
"jshint": "*",
"lodash": "*",
"merge-stream": "^0.1.7",
"q": "*",
"request": "*",
"run-sequence": "^1.0.2",
"stringify": "^3.1.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"vinyl-transform": "^1.0.0"
}
How do I ensure that these files are preserved upon npm install? I know for a fact this used to work in earlier versions of npm but I haven't seen anything in the changelogs.
NodeJS Version = v5.4.1
NPM Version = 3.3.12
Thanks in advance for the help!
When npm install is run, it will examine the package.json file and attempt to install any dependencies listed that are not already installed to the node_modules directory. If there are no dependencies listed, it replaces the entire node_modules directory.
To preserve the files in the node_modules directory, you can do the following:
Install a module and have it added to the dependencies list in the package.json.
Move the symlink files into the module's folder.

NPM is installing lots of libraries

When I use npm install base from package.json file it installs 337 libraries :/
Here is my package file
{
"name": "system_glowna",
"version": "2.0.0",
"dependencies": {
"angular2": "^2.0.0-beta.0",
"bootstrap": "^3.3.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.0",
"systemjs": "0.19.6",
"zone.js": "0.5.10"
},
"devDependencies": {
"del": "^1.2.0",
"gulp": "^3.8.11",
"gulp-autoprefixer": "^2.2.0",
"gulp-concat": "^2.5.2",
"gulp-inject": "^1.2.0",
"gulp-less": "^3.0.3",
"gulp-load-plugins": "^0.10.0",
"gulp-minify-css": "^1.1.6",
"gulp-minify-html": "^1.0.3",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.3",
"gulp-uglify": "^1.2.0",
"gulp-watch": "^4.2.4",
"run-sequence": "^1.1.0"
}
}
Why it's installing so many libraries? How to limit them?
npm prior to version 3.x installs libraries in a tree structure, so each library has its own node_modules and so on. It could lead (or almost always leads) to duplicated libraries inside different node_modules packages and results in a pretty big node_modules folder. Things get tricky e.g. on Windows, when paths could be too long to delete node_modules easily after.
However, after 3.x release of npm, it uses flat folder structure by default and only if there is any conflict, it installs proper versions for each library.
Note to yourself - the fact that npm installs so many libraries is necessary in order for each library to work properly :)
You've told it to install a fairly large number of libraries. Each of those libraries has its own dependencies, which are necessary for that library to run. You can't limit or prevent that unless you just don't want to use the library in question.
Note that installing lots of libraries isn't, by itself, a problem.

Resources