How can I install devDependencies of one of my devDependency? - node.js

In a Node project, I want to install all devDependencies of one devDependency of my project.
By default NPM/Yarn will install only the dependencies of my dependencies, but not the devDependencies.
Is there a way to install all devDependencies of a particular devDependency?

What I ended doing is using a postinstall script in package.json:
"scripts": {
"postinstall": "cd node_modules/the-package-i-want-all-dependencies-installed && yarn",
},

yarn add -D <package...>
https://classic.yarnpkg.com/lang/en/docs/cli/add/#toc-yarn-add-dev-d

Related

npm install everything except named package

An extract from my Dockerfile which builds my Angular app:
# Do other stuff
RUN npm install
# next line executes ng build --prod
RUN npm run build:prod
The npm install is for build purposes. Some of the packages in devDependencies - particularly cypress - take ages to install and are not needed for the build. However, some packages in devDependencies are needed.
Can I, for example, do an npm install everything except cypress?
npm install --only=prod is not an option because some devDependencies are needed.
Thanks in advance for your thoughts.
Just a suggestion. Use install-subset, and can be installed globally with npm install -g install-subset
To use it, you build inclusion lists and exclusion lists for named installation subsets in your package.json like this:
"subsets": {
"build": {
"include": [
"babel-cli",
"dotenv"
]
},
"test": {
"exclude": [
"eslint",
"lint-rules",
"prettier"
]
}
}
Then run install-subset test
This will temporarily rewrite your package.json to not install those packages excluded, then restore it (very similar to how lerna operates), which depending on the packages can save a lot of time and bandwidth.

npm-force-resolutions not working when installing a new package

I'm using the scripts section of the package.json to force resolutions:
"preinstall": "npx npm-force-resolutions"
in the resolutions section, I have entered graceful-fs with a specified version:
"resolutions": {
"graceful-fs": "^4.2.4",
},
When i run npm i everything is installed correctly, the set versions are taken in to account. But later on when I install an additional module, e.g. npm i random-package, my set versions are being thrown away and I endup with graceful-fs#1.2.3 and other low versions in some dependencies.
If I clear the node_modules folder and run npm i again, everything is alright again.
I also tried setting the resolution more specific, like
"resolutions": {
"glob/**/graceful-fs": "^4.2.4",
},
but this doesn't help.
I also tried:
adding the module as dependency, devDependency or peerDependency
using a shrinkwrap and overriding it there
but no luck.
what am I missing?
The best solution for me to automate this was modifying preinstall script as above:
"preinstall": "npm install --package-lock-only --ignore-scripts && npx npm-force-resolutions",
Best way is to change the preinstall script to this:
"preinstall": "([ ! -f package-lock.json ] && npm install --package-lock-only --ignore-scripts --no-audit); npx npm-force-resolutions"
This will only run npm install to create your initial package-lock.json when it does not exist yet.
This is much faster than always running both (npm + npx).
As of npm 8.3.0, you can also use npm's override:
{
"overrides": {
"graceful-fs": "^4.2.4"
}
}
in the resolutions section, you must fix version
"resolutions": {
"graceful-fs": "4.2.4",
},
Hi #NthDegree the only way which worked for me was to first run the normal npm install and then add the packages-lock.json file to git. After doing that when you add "preinstall": "npx npm-force-resolutions", it always updates the dependency resolution to the version mentioned.
I am not sure if adding packages-lock.json file to git is good or bad but by using this method the CI/CD pipeline works as well.
If all of the above answers don't work and you still get sh: npm-force-resolutions: command not found
try the following:
Just change:
"preinstall": "npx npm-force-resolutions"
To:
"preinstall": "npx force-resolutions"
npx force-resolutions does not run when no package-lock.json is detected, and allows the next command inline to be executed as normal
Credit to: https://github.com/rogeriochaves/npm-force-resolutions/issues/10#issuecomment-885458937

package.json scripts that work with npm and yarn?

I am using npm as a build tool and so in my package.json, and some of my scripts depend on other scripts:
{
"test": "npm run lint && mocha"
}
This hardcodes the npm package manager into package.json. How can make this approach to expressing dependencies work with both npm and yarn?
The $npm_execpath environment variable refers to the build tool, so just replace npm with the $npm_execpath:
{
"test": "$npm_execpath run lint && mocha"
}
Both npm test and yarn test will work, and will use the appropriate build tool.
While mjs' answer is great, there's also a small package that is purported to work on all environments including Windows: https://www.npmjs.com/package/yarpm
To use in a project, run yarn add yarpm --dev / npm i -D yarpm and then just use yarpm in your scripts like this:
{
"test": "yarpm run lint && mocha"
}
As the package README notes, you just need to make sure your commands would be suitable for passing through to either yarn or npm: you cannot use arguments/flags that only work on one package manager.

Execute script after package installation

Now, I there is postinstall to run a script after
npm install
However, I am looking for a way to run a script after installing a single package
npm install jquery --save
Is this possible? If so, does this work on Windows and is there a way to get the name of the installed package (jquery in the given example)?
I'm don't see this on the package.json features.
It is therefore possible to do in the prestart for instance but jquery won't be in the devDependencies that way:
{
...
"devDependencies": {
"bower": "1.3.x",
"uglifyjs": "2.4.10",
... your other dependencies
},
"scripts": {
"prestart": "npm install jquery##.#.# ; <yourcommand> ; npm install",
...
}
}

How can I invoke npm on heroku command line (to install bower components)?

Bower is for client side Javascript what npm is for the server side and reads a component.json file to recognize dependencies that should be fetched at deploy time so I'd be happy it heroku would run it at slug compilation time.
Unfortunately I can not invoke npm or bower from a heroku console or one-off command (heroku run "npm help") (heroku run bash -> npm help) as it's possible with ruby's rake. I've put npm and node (latest/x versions) in my package.json but in the engines section, not the dependencies.
I think this could be solved by customizing the node buildpack but I consider this a little too heavy task just for activating something so obvious.
You can also setup a postintall command, something like this in your package.json
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Then npm install will also install bower dependencies.
Pros : one command to rule them all.
Cons : you unnecessarily embed bower as a dependency.
You can use run like this:
heroku run npm install git://github.com/webjay/kaiseki
You should declare NPM dependencies in the package.json file
All you install from shell will be deleted on exit shell. You are in a cloned instance.
You can use bower directly like this
"dependencies": {
"bower": "^1.7.9"
},
"scripts": {
"postinstall": "sudo bower install --allow-root "
}

Resources