DEV Community

Cover image for Use npm pack to test your packages locally

Use npm pack to test your packages locally

Stephen Cooper on February 18, 2022

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...
Collapse
 
abbasc52 profile image
Abbas Cyclewala • Edited

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

Collapse
 
scooperdev profile image
Stephen Cooper

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.

Collapse
 
receter profile image
Andreas Riedmüller • Edited

I didn't know about this method, I always use npm link.
For developing this is very handy as it is also possible to get autorefresh/hmr

For testing a package before publishing using npm pack seems like a much better alternative because npm link does not care about the files property. All files in the project are included as it is just a symlink.

And I just found out that this should work as well:

npm install https://github.com/username/repo.git
Enter fullscreen mode Exit fullscreen mode

Thanks for sharing!

Collapse
 
d4nyll profile image
Daniel Li

npm link uses symbolic links to link to raw source files as opposed to what the end-users will see, which is usually the build artifacts and maybe a subset of the source files copied over. Using npm link does not ensure you've packaged your package correctly.

Collapse
 
kuabhish profile image
Abhishek Kumar

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

Collapse
 
scooperdev profile image
Stephen Cooper

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

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

Collapse
 
receter profile image
Andreas Riedmüller

I tried to summarize the important points in an article of mine, you can find it here: dev.to/receter/the-minimal-setup-t...

Collapse
 
sarveshprajapati profile image
Sarvesh Prajapati

great article man

Collapse
 
scooperdev profile image
Stephen Cooper

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.

Collapse
 
sarveshprajapati profile image
Sarvesh Prajapati

absolutely

Collapse
 
adampatterson profile image
Adam Patterson

With PHP Composer I can keep my code and dependance linked to Packagist and just replace the folder with a SimLink locally while I develop my code.

With NPM this won't work. I have to use "file:../_packages/my-package-name" as you mentioned but now my CI build is broken...

I must be doing something wrong or this seems like a really big inconvenience.

Collapse
 
ge6ben profile image
Lara Raoof

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

Collapse
 
ge6ben profile image
Lara Raoof

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

Collapse
 
ramstheroyalp profile image
ramstheroyalp • Edited

@scooperdev Thanks for the blog...
Btw, do we need to provide a relative path here?

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

What if the calling application is a different project altogether? How too reference this file?
May be copying it to the project and keep it at the package.json level and call?