npm pack for local package dependency testing – The Global Tofay

npm pack for local package dependency testing - The Global Tofay Global Today

Steps to test npm package dependencies locally without publishing to npm.

Installing the packed tarball works exactly like installing a package from an npm repo whereas linking to the local package with npm link doesn’t always behave the same as a real repo.

Build and pack local package

The npm pack command generates a tarball (.tgz) of the built project with the name and version from the package.json file (e.g. my-local-package-1.0.0.tgz).

  1. cd ~/Projects/my-local-package
  2. npm i
  3. npm run build
  4. npm pack

Install local package in web app

  1. cd ~/Projects/my-web-app
  2. npm i ../my-local-package/my-local-package-1.0.0.tgz
  3. npm start

Extra step for Next.js apps

Might need to clear the Next.js cache by deleting the .next/cache folder before installing the local package (step 2 above).

rm -rf .next/cache

 

#npm #pack #local #package #dependency #testing

Leave a Reply

Your email address will not be published. Required fields are marked *