Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

Today, I read the article Making it Easier to Work With Local npm Packages written by Aaron Parrel and learned how to quickly link local node modules.

Aaron describes that you can specify local packages and 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 package which the file: prefix already unveils.

When would you rely on local packages?

The primary use case for local packages is package development. If you're working on an npm package that will be released in the registry, you need a way to test your changes before making the source code available to the world.

And ideally, you want to test your new code from within a project that uses your soon to be released project as a dependency, too. That's the moment when you need a way to reference this local package from within another project.

npm install supports local directories and packages

Using local package paths and the file: syntax feels intuitive. After reading more about this approach, I discovered that the npm install command supports local packages, too. ๐Ÿ˜ฒ

npm install ../some-local-package

The above install command adds some-local-package to your package.json's dependencies. The local package definition will then include the file: prefix. Additionally, it'll create a symlink in your node_modules directory pointing to the local package. That saves a lot of work and is quickly done!

Side note: if you want to install a local package, the defined package path has to include a valid package.json โ€“ otherwise, npm install will fail.

I have to say, it's pretty handy that npm offers this functionality to develop local packages and dependencies! It removes the need for manual symlink creation, and let me be honest here, I never make it to create a symlink on first try. ๐Ÿ™ˆ

Local module install

If you want to find more Node.js tips and tricks head over to the Node.js section on my blog.

If you enjoyed this article...

Join 5.4k readers and learn something new every week with Web Weekly.

Web Weekly โ€” Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles