Beetlejuice
Beetlejuice

Reputation: 4425

Build and use npm package locally

I made some custom modifications in the ngx-mask package and need to test it locally.

How to overwrite the installed npm package?

Currently the package is declared as a dependency in packages.json file as:

"ngx-mask": "^7.8.9"

I do prefer not to fork the original package and not to use github for this, if possible.

Upvotes: 34

Views: 65937

Answers (3)

akop
akop

Reputation: 7835

'npm install'

npm link should be avoided because of different bad behaviours. Read this blog-article for more informations https://hirok.io/posts/avoid-npm-link

Here is an alternative with npm install:

  1. Build your ngx-mask project. You should have a dist-folder after that.
  2. Add ngx-mask to the dependencies of your test-project. Use the current version of package.json of ngx-mask.
  3. Run npm install --no-save /path/to/ngx-mask/dist

If you have multiple packages, then you have add them with one command.

# good
~ npm i --no-save /path/packageA/dist /path/packageB/dist

# not good
~ npm i --no-save /path/packageA/dist
~ npm i --no-save /path/packageB/dist

Upvotes: 1

James
James

Reputation: 2884

If you have made these changes on your machine. (I'm assuming you have)

  1. Run a build of the ngx-mask package that you changed.

  2. run npm pack from that package's root folder. This creates a .tgz zip file of your package with your custom modifications.

  3. copy that file into the root (you could put it wherever but root makes things easy) of your project.

  4. in your package.json replace the version number ngx mask to the following "ngx-mask": "file:my-packed-file.tgz"

  5. Run an npm install using your new package.json

you should have your modified copy loaded in as a dependency in node_modules.

Upvotes: 77

rouble
rouble

Reputation: 18171

The 'npm link' command was made for this.

In your test repo (where you use the ngx-mask package) run:

npm link /path/to/your/locally/modified/ngx-mask/package

This will install your locally modified ngx-mask into your test repo.

When you are done testing your local version of the ngx-mask package, you can simply unlink it. To unlink the local version of ngx-mask, in your test repo run:

npm unlink --no-save /path/to/your/locally/modified/ngx-mask/package

If you want to re-install the registry version of the ngx-mask package run:

npm install

Upvotes: 26

Related Questions