Can I force a specific version for a nested NPM package dependency?

I’m trying to use awesome-test-runner in my project but I’m running into issues. It depends on cool-lib-runner which uses an old version of ghost-browser. This old version has a bug that stops it from working on my Mac.

I know the latest ghost-browser fixes this problem. But awesome-test-runner is stuck using the old one because of how the dependencies are set up.

I’ve tried adding ghost-browser to my own package.json but it doesn’t help. Both versions get installed and awesome-test-runner still picks the old one.

Is there a way to make cool-lib-runner use the newer ghost-browser? Or maybe force awesome-test-runner to use a different version of cool-lib-runner?

Here’s what my dependency tree looks like now:

[email protected]
└─┬ [email protected]
  └── [email protected]

Any ideas on how to solve this would be great. I really want to use awesome-test-runner but this version problem is holding me back.

I’ve dealt with similar nested dependency issues before, and it can be tricky. One approach that’s worked for me is using npm’s ‘resolutions’ field in your package.json. It lets you override nested dependencies.

Add something like this to your package.json:

"resolutions": {
  "ghost-browser": "^3.0.0"
}

This tells npm to use the specified version for all instances of ghost-browser, even in nested dependencies. You might need to clear your npm cache and node_modules folder, then reinstall everything.

If that doesn’t work, you could try using Yarn instead of npm. It has a similar feature called ‘selective dependency resolutions’ that can be more reliable for deep overrides.

As a last resort, you might need to fork cool-lib-runner and update its ghost-browser dependency yourself. It’s not ideal, but sometimes it’s necessary when maintainers are slow to update.

Have you considered using npm’s ‘overrides’ feature? It’s designed for situations like this where you need to control versions of nested dependencies. You can add an ‘overrides’ section to your package.json file:

"overrides": {
  "ghost-browser": "^3.0.0"
}

This should force all instances of ghost-browser to use the specified version, regardless of what other packages require. It’s generally more reliable than the older ‘resolutions’ approach.

If that doesn’t work, you might need to reach out to the maintainers of awesome-test-runner or cool-lib-runner. They might be willing to update their dependencies if you explain the issue. Sometimes, direct communication can lead to quicker solutions than trying to work around the problem yourself.

hey mate, have u tried npm-force-resolutions? it’s a nifty tool that can override nested dependencies. Just add a resolutions field to ur package.json with the version u want:

“resolutions”: {
“ghost-browser”: “3.0.0”
}

then run npm-force-resolutions before npm install. might do the trick without messin with the original packages.

@Liam23 npm-force-resolutions was replaced by “overrides” in npm 8.3, it is recommended to use the native “overrides” going forward.

“resolutions” only works with yarn, not npm. For npm use “overrides”. Note that overrides can be nested, e.g:

{
  "overrides": {
    "@npm/bar": {
      "@npm/foo": "1.0.0"
    }
  }
}

to only override @npm/foo to be 1.0.0 when it’s a child (or grandchild, or great grandchild, etc) of the package @npm/bar.