When working with npm packages, it is good to be able to test it locally without the necessity of publishing it to npm. Here are some ways you can achieve that.
Use npm link
You can create a symlink like this.
In the package:
npm link
In the project that uses the package:
npm link my-package
Now the packages are linked.
To unlink:
npm unlink my-package
Install locally
Install by just referencing the local path inside package.json
{
"name": "my-project",
"dependencies": {
"@myuser/my-package": "file:../lib"
}
}
And then just install
npm i
Install locally by using npm pack
If you want, you can package the local package and use it locally. Here you are using the same artifact that gets uploaded to npm.
npm pack
This command is generating this file my-package-X-X-X.tgz
. Where X.X.X is the semver version.
You can now reference this tarball directly in your main project inside package.json
"dependencies": {
"@myuser/my-package": "file:../lib/my-package-X-X-X.tgz"
}
Use the package
import "@myuser/my-package/dist/index.css"
Just one example of importing a file from the package.
Bonus: Pre-install a local package with the main project install
Maybe you are developing a package and making a Documentation website for it, or another project that is using lib
.
.
├── docs
├── lib
Then you can install the package’s dependencies upon installation in the docs
project. Using the preinstall
built in npm script, it will install this first upon npm i
in he docs project.
"scripts": {
"preinstall": "npm install ../lib/"
}