Use npm pack to test your packages locally (2023)

Use npm pack to test your packages locally (1)

Use npm pack to test your packages locally (2)

Stephen Cooper

Posted on • Updated on

Use npm pack to test your packages locally (3) Use npm pack to test your packages locally (4) Use npm pack to test your packages locally (5) Use npm pack to test your packages locally (6) Use npm pack to test your packages locally (7)

When working with npm packages you often want to test your package without having to publish it to npm. This enables you to have a quicker feedback loop and keeps the number of published versions to a minimum.

This is where npm pack comes in.

First: Build your Package

Before you can use npm pack you must first build your package. So run your build command and check that this has completed successfully. In my use case, for @ag-grid-community/angular the command is:

community-modules/angular$ npm run build

Second: Locate your Build Artifacts package.json

The important part of this step is to run npm pack in the correct folder location where the build artifact package.json is located. Depending on your project that may be the same folder as where you ran your build. For me though, with this Angular library that means navigating into the dist/ag-grid-angular folder as that is where the Angular library builds to.

community-modules/angular$ cd ./dist/ag-grid-angular

Third: Pack your artifacts

Now it is time to package our build artifacts to enable us to produce a package that mimic what would be published to npm. We tell npm to pack it to our user folder ~ for ease.

community-modules/angular/dist/ag-grid-angular$ npm pack --pack-destination ~

After this runs you will have the following file.

~/ag-grid-community-angular-27.0.0.tgz

Fourth: Point package.json to your file

Now that we have this package we can test it in our application. To do this we update our package.json to point to the file that we produced in the previous step.

"dependencies": { "@ag-grid-community/angular": "file:~/ag-grid-community-angular-27.0.0.tgz"}

Then make sure you remember to re-install your packages after this update.

npm install

Fifth: Test your package in your application

Now you can run your application using your packed dependency.
This enables you to test your package without having to publish it to npm for all the world to see.

Not only does this save us time by giving us a quicker feedback loop but it keeps our published packaged versions clean. This approach also gives us more confidence that our library is working as expected when installed into an application. We are more closely mimicking our users as opposed to testing against the raw source files.

Happy testing!

Stephen Cooper - Senior Developer at AG Grid
Follow me on Twitter @ScooperDev or Tweet about this post.

Top comments (9)

Subscribe

Use npm pack to test your packages locally (9)

Great article! Just adding some more points which might be helpful.

You don't need to pack your library to test it.
Just point the package.json dependency to actual library folder instead of the packed version and do npm i once.
npm creates a link to actual folder inside node_modules

Saves you additional steps of packaging and installing it after every change in your test project

Use npm pack to test your packages locally (11)

Stephen Cooper

Senior Developer AG Grid building out the JavaScript grid.Tech Blogging, Father of 4

  • Location

    London

  • Work

    Senior Developer at AG Grid

  • Joined

Thanks for highlighting this use case. I have done this in the past but seemed to run into issues with the linking. Maybe I should give it another go though as does save a few steps.

However, I still like the additional confidence of working off the same item that will actually end up in npm.

Use npm pack to test your packages locally (13)

How to do this. Can you explain with code. Please.

Use npm pack to test your packages locally (15)

Stephen Cooper

Senior Developer AG Grid building out the JavaScript grid.Tech Blogging, Father of 4

  • Location

    London

  • Work

    Senior Developer at AG Grid

  • Joined

The official docs have some examples of this with an explanation of what it does.

docs.npmjs.com/cli/v8/commands/npm...

Use npm pack to test your packages locally (17)

Sarvesh Prajapati

Owner & Creator @WithinBracket.com. Twitter @iamthesarvesh

  • Location

    India

  • Work

    Content Creator @ WithinBracket

  • Joined

great article man

Use npm pack to test your packages locally (19)

Stephen Cooper

Senior Developer AG Grid building out the JavaScript grid.Tech Blogging, Father of 4

  • Location

    London

  • Work

    Senior Developer at AG Grid

  • Joined

Thanks! I have come back to the draft a few times to remind myself so how to do it so thought I should share it.

Use npm pack to test your packages locally (21)

Sarvesh Prajapati

Owner & Creator @WithinBracket.com. Twitter @iamthesarvesh

  • Location

    India

  • Work

    Content Creator @ WithinBracket

  • Joined

absolutely

Use npm pack to test your packages locally (23)

God bless u!
I should delete my node_module and package_lock.json to see the changes!
when I test it again

Use npm pack to test your packages locally (25)

Also Do you have any idea about this question?
Thanks in advance

For further actions, you may consider blocking this person and/or reporting abuse

Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated: 21/01/2024

Views: 5533

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.