npm install supports local packages and dependencies
- Published at
- Updated at
- Reading time
- 2min
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.
{
"dependencies": {
"durable-functions": "file:../azure-functions-durable-js",
}
}
This package 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.
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.
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'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 โ 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. ๐
If you want to find more Node.js tips and tricks head over to the Node.js section on my blog.
Join 6.1k readers and learn something new every week with Web Weekly.
