How to NPM Link to a local version of your dependency

_Aid();
3 min readJan 30, 2019

--

Lets do this.

So… I want to create a local version of moment for my calendar webapp.

Why?

Because moment doesn’t include Klingon representations of time. Or Borg, for that matter. What kind of a all-inclusive time package doesn’t embrace foreign cultures!?

The TL;DR: clone the package locally somewhere, npm install its deps, npm link at its root, then go back to your flashy app that uses moment, at root type npm link "moment"& you’re now using the local version. I couldn’t get yarn links to work. Not sure why.

Right…

I’m abandoning Yarn in this explanation because i couldn’t get the same functionality to work with it as NPM. So anyway, here goes:

I want two folders to focus on.

my-calendar-app/node_modules/moment

and

Users/aid/desktop/local-modules/

Then I git clone... the package i want to edit/improve, *into* the local-modules folder.

Once i’m in that, i run npm install to ensure i have all the right dependencies and config for that (babelrc, webpack, dev and regular dependencies).

In the local one, I go into moment.js or whatever the entry file is. And right at the top I put a console.log in, just to show that we’re using the local/Klingon-friendly one:

Users/aid/Desktop/local_modules/moment/moment.js

cd (in terminal) into your new, local modules/moment package dir and at its root, type npm link. To be clear: this is you linking a local, cloned, slightly changed version of an npm package.

So we’ve created an index link to this local package. The terminal confirmation for mine looks like this:

after typing npm link in the new, local package.

Now navigate back to your main project, and at root level, tell it you want to use the local version:-

cd /Users/aid/Projects/my-calendar-app && npm link "moment"

That spits out a confirmation that you are mapping to a local module:

Users/aid/Projects/my-calendar-app/node_modules/moment -> aid/.nvm/versions/node/v10.15.0/lib/node_modules/moment -> /Users/aid/Desktop/local-packages/moment

That’s basically saying “When i’m pointing at node_modules for moment, root to your npm links, for this version of node — and point to this local version/package of it”.

Now when we run our app:

The chrome console / console.logs are appearing.

It spits out the console.logs / changes we made locally.

To kill the link, go back to your local version and at root type npm unlink --no-save moment. Then run npm install.

Yay for NPM links. Yay for Des Lynham.

--

--

_Aid();

I Learn Stuff & Say Stuff. And Code Stuff. And stuff.