npm install supports local packages and dependencies
- Published at
- Updated at
- Reading time
- 2min
This post is part of my Today I learned series in which I share all my learnings regarding web development.
Today, I read the article Making it Easier to Work With Local npm Packages written by Aaron Parrel. He 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.
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 very intuitive. After reading more about this approach, I discovered that the npm install
command supports it, too. 😲
npm install ../some-local-package
The above install
command will add 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. 🙈
If you want to find more Node.js tips and tricks head over to the Node.js section on my blog.