How to resolve dependencies in a nodejs package - node.js

I've downloaded a nodejs project from Github, edited the source to add missing functionality and now I'm trying to compile it into an executable or run it uncompiled (node main.js). In both cases I get messages that it can't find its dependencies.
This is not my normal programming language, so I'm unfamiliar with how node dependencies work.
The dependencies in questions are other node packages. I installed them with npm install -g but that didn't help.
In the package.json file they are described as follows:
"dependencies": {
"#castlelemongrab/ioh": "^0.0.4",
"#castlelemongrab/strr": "^0.0.1",
"bent": "^7.3.6",
"jsDump": "^1.1.0",
"yargs": "^15.3.1"
},
The warning messages specify the dependencies beginning with # as the ones that are missing. Although it's just a warning, the program fails to function and issues the same message when you attempt to use it, so it's effectively an error. I found this Stackoverflow post mentioning that you could define dependencies like this:
"dependencies": {
"public": "git://github.com/user/repo.git#ref",
"private": "git+ssh://git#github.com:user/repo.git#ref"
}
But that didn't seem to have any effect:
Error: Cannot find module '#castlelemongrab/ioh'
How should I resolve it? I tried going into the node_modules directory and git clone-ing the ioh library into the #castlelemongrab folder but that also led to some errors:
npm ERR! code EISGIT
npm ERR! path C:\...\node_modules\#castlelemongrab\ioh
npm ERR! git C:\...\node_modules\#castlelemongrab\ioh: Appears to be a git repo or submodule.
npm ERR! git C:\...\node_modules\#castlelemongrab\ioh
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.
Additionally, since I have the production version of this package installed on my system and I don't want to overwrite it, if I change the "name" field in package.json is that an effective and safe way to avoid conflicts?

The solution was to git clone the missing modules into node_modules and then to rm -rf the .git and .gitignore.
Figured this out by trial and error. If anyone has a more complete and informed answer, please feel free to add it.

Related

Unable to fix npm vulnerabilities

I am getting 6 vulnerabilities after running npm audit report:
I tried a solution and overridden the vulnerable versions of a particular package with their latest versions in package.json file like this:
"overrides": {
"nth-check": "2.1.1",
"#svgr/webpack": "6.5.1",
"#svgr/plugin-svgo": "6.5.1",
"svgo": "3.0.1",
"css-select": "5.1.0"
}
Then I updated the npm packages with npm update. But it did not change the result.
Tried another solution by making a resolution object in package.json and specified specific versions of a particular package, and ran it using npx i npm-force-resolutions but it gives this error:
npm ERR! could not determine executable to run.
But I am still unable to fix the npm vulnerabilities. Please help!
You should delete both node_modules and package-lock.json before launching npm install again; this will require more time to install all dependencies, but this will override all the version that are currently installed (it will bring also minor updates in dependencies).
Also, for this vulnerability, you only need to override nth-check. You can see the changes by executing npm list nth-check with and without the override (remember to delete both node_modules and package-lock.json).

Is it okay to install `npm` as a dependency?

We want to use AsyncAPI to document our RabbitMQ messaging. Therefore, we installed asyncapi/generator as a npm dependency.
If you have a look at the package.json you can see that it references npmi as a dependency which in turn is referencing to global-npm. If we want to run it, a globally installed node and npm is necessary.
Now if we run the generator ($ ag ./docs/asyncapi.yaml #asyncapi/html-template --output ./docs/asyncapi/ --force-write) on a machine which has no globally installed npm following error message appears:
/path/to/project/node_modules/global-npm/index.js:13
throw err
^
Error: Cannot find module 'npm'
at throwNotFoundError (/path/to/project/node_modules/global-npm/index.js:11:13)
at /path/to/project/node_modules/global-npm/index.js:39:5
...
As a workaround we declared npm itself as a dependency:
"dependencies": {
"#asyncapi/generator": "^1.1.4",
"#asyncapi/html-template": "^0.15.4",
"#asyncapi/markdown-template": "^0.11.1",
"npm": "^6.14.9",
...
I've never seen such a thing. Is this acceptable or do we need to install our npm on our machines separatly?
if you run ag you must have installed it with npm initially right? so npm is most probably on this machine already.
The error you have, I saw it on windows only, when you have the generator as dependency, and most likely you use nvm.
Solution was this, so manual bump of global-npm to have this fixed in npmi. This is a workaround,long term I think we need to get rid of npmi dependency from the generator I think
We only had this issue in our CI/CD pipeline working with maven-frontend-plugin which is installing node/npm. npm is located in node/node_modules. This is no location where AsyncAPI looks for npm. In order to fix this issue we link the npm-cli.js (which is npm) from
the maven-frontend-plugin to a well known place where AsyncApi looks up for npm node_modules/.bin.
- ln -sf "$(pwd)/node/node_modules/npm/bin/npm-cli.js" "$(pwd)/node_modules/.bin/npm"

Npm Install straight from package.json

I have a simple question that i cant seem to find the answer for. I cloned a git repo to my local machine.
When attempting to start node, I receive an error because i don't have the required npm dependencies installed. However, they are located in the packages.json file that was cloned.
I was wondering if there was a simple way to install the dependencies located in that file, without having to npm install for every individual package.
Within the directory of the package.json file, just run npm install. It will read package.json and install all dependencies. If you want to limit it to only non-dev dependencies, use npm install --only=production.

EINTEGRITY: npm 5.0 integrity check and modernizr.com dependency

I've encountered this error when installing deps of my package:
$ npm i
npm ERR! code EINTEGRITY
npm ERR! sha1-tU7jWojzuU8MIY2VLAx+BwluNo0= integrity checksum failed when using sha1: wanted sha1-tU7jWojzuU8MIY2VLAx+BwluNo0= but got sha1-oXYP0kzpbhku0KU+phy353lbBhQ=. (26624 bytes)
npm ERR! A complete log of this run can be found in:
npm ERR! /home/tlenex/.npm/_logs/2017-06-22T10_18_19_773Z-debug.log
the problem is with my Modernizr dependency:
"dependencies": {
"Modernizr": "https://modernizr.com/download?setclasses-flash"
}
is there any way to solve this or ignore this integrity check?
Currently I have to run
npm i https://modernizr.com/download?setclasses-flash
again to get things working, which overrides the "integrity" field for "Modernizr" in my package-lock.json.
This may happen every time there is a change in Modernizr package fetched from this link and my package dependencies need to be reinstalled (for example, each time on CI build)
If there is no other way of solving this? I hope I wont have to place package-lock.json in my .gitignore file :(
More data about my enviroment:
$ npm -v
5.0.3
$ node -v
v6.11.0
Edit package-lock.json , find the one you want to skip in this case the one that its failing
sha1-tU7jWojzuU8MIY2VLAx+BwluNo0
and remove the integrity parameter from it i.e
},
"range-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz",
"integrity": "sha1-9JvmtIeJTdxA3MlKMi9hEJLgDV4=",
"dev": true
},
to...
},
"range-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz",
"dev": true
},
after that run npm install, will check the rest, skip this integrity
The point of the integrity field is to alert you when something has changed, so if you do not want it to exist, you can disable package-lock.json files in your npmrc. Just set package-lock=false
Note: I am the developer of Modernizr, and spoke with the npm-cli team about this issue. The root cause appears to be the change of the SHA type between npm5 and earlier versions. Nuking the node_modules folder will fix it
Find all outdated packages and update theme:
npm outdated -g
sudo npm i -g outDatedPKG
Upgrade npm to lateste version with:
sudo npm i -g npm
Delete package-lock.json file.
Delete _cacache directory in ~/.npm:
npm cache verify
4.1. Every time i get that error, do steps 2 & 3.
If you still get the error, clear npm's cache:
npm cache clean --force
I had this same error and I solved it by :
Deleting package-lock.json
Running "npm install"
I finally resolved this issue.
Our team moved away from URL dependency without SEMVER notation, in this case https://modernizr.com/download?setclasses-flash and used modernizr-loader with webpack. There are also equivalents for gulp and grunt tools available on npm, pick and use one you like the most.
After using them, we finally get rid of returning EINTEGRITY npm error without nuking package-lock.json or node_modules.
Just do two things for the solution
first :
npm cache clean --force
second :
npm i -g npm
and than install what u want
$ rm -rf package-lock.json node_modules
$ npm install --cache /tmp/empty-npm-cache
If this fixes it, clear your global npm cache to fix the corruption.

Install NPM package as Git clone

I'm working on a project that is using one of my own libraries as a dependency. I'm still making lots of changes to my library and therefore I want to require my library as a Git clone.
From the NPM Install documentation get the impression that this is possible [1] but when I add my dependency and I run an npm install the dependency is added without any of the Git information I require.
"dependencies": {
"mylib": "bitbucket:acme/mylib#dev-master"
},
How can I make NPM do a Git clone in my node_modules directory rather then just getting the files?
[1] https://docs.npmjs.com/cli/install
You can check this answer, and modify it for your repo. It works for almost all cases.

Resources