Reliably installs a local package into another, for testing.
It’s very similar to installing a published package; npm pack is used to generate a tarball that only includes published files, which are extracted into node_modules for a similar result to a regular npm install --no-save.
npm link is a notoriously unreliable way to locally test a package in another package, as it uses symlinks:
- It will not reveal if the
package.jsonfilesfield or.npmignoreis correct, asnpm linkdoesn’t usenpm packlikenpm publishdoes. - As the package being tested is not actually installed inside the
node_modulesof the package it’s being tested in, peer dependencies installed in the host package can’t be reached. For example, this makes it difficult to test a package that has areactpeer dependency inside a Next.js project. - How it works is not intuitive. It messes with the global
node_modulesandbindirectories, and you have to remember to usenpm unlinkto tidy up, which inconveniently strips the dependency from yourpackage.json.
Installation isn’t required to use the CLI command install-from with npx.
To install with npm, run:
npm install install-from --save-devThen, use either the CLI command install-from or the JS API function installFrom.
Reliably installs a local package into another at the CWD, for testing.
| Argument | Description |
|---|---|
| 1 | Absolute or CWD relative filesystem path to the package to install from. |
Installing a local package into another using npx.
In a terminal, change to the directory of the package you want to test another package in:
cd ~/Repos/fooThen, install the desired local package:
npx install-from ~/Repos/bar
Reliably installs a local package into another, for testing.
| Parameter | Type | Description |
|---|---|---|
pathPackageFrom |
string | Absolute or CWD relative filesystem path to the package to install from. |
pathPackageTo |
string? | Absolute or CWD relative filesystem path to the package to install to, defaulting to process.cwd(). |
Returns: Promise<void> — Resolves once installation is complete.
How to import.
import installFrom from 'install-from';
Installing a local package into another.
installFrom( './packages/package-to-install-from', './packages/package-to-install-to' ) .then(() => { // … }) .catch(() => { // … });