npm install supports local modules

Published at
Updated at
Reading time
2 min

This post is part of my Today I learned series in which I share all my learnings regarding web development.

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",
    }
}

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

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!

Related Topics

See null comment.