DEV Community

Stefan Judis
Stefan Judis

Posted on • Originally published at stefanjudis.com on

TIL: npm install supports local modules

Today, I was reading the article Making it Easier to Work With Local npm Packages written by Aaron Parrel. He describes that you can specify local modules right in your package.json.

{
    "dependencies": {
        "durable-functions": "file:../azure-functions-durable-js",
    }
}
Enter fullscreen mode Exit fullscreen mode

This package.json example includes a durable-functions package. It is not installed from npm, though. durable-functions is a local module which the file: prefix already unveils.

When can this be useful?

The primary use case for local modules is module development. At some point, you want to test your new module inside another module/package/site. This moment is when you need a way to reference this local module from within another project.

As Aaron points out, one way to make that work is to use npm link. Unfortunately, my experience with npm link is only so-so. I guess it's the same as using ln -s to create a symlink – you never get it right the first try!

Local module paths feel more intuitive to me. After reading more about them, I discovered that npm install supports them, too. 😲

npm install ../some-local-module
Enter fullscreen mode Exit fullscreen mode

This install command will add some-local-module to your package.json's dependencies. The local module path will include a file: prefix. It'll also create a symlink pointing to your local module to your node_modules. Pretty sweet!

The defined module path has to include a valid package.json – otherwise, npm install will fail.

I have to say that's very handy when dealing with local modules!

Discussion (0)