🖖 Greetings, this site is prototype for low code CMS deployments: Discover MoreStar on GitHub

SKNOW.IT

Cover Image for How to specify local modules as npm package dependencies

How to specify local modules as npm package dependencies

While specifying local modules as npm package dependencies shouldn’t be done in production or published packages. Sometimes, when doing development locally, you may want to use local modules that are not published to the npm registry.

For this to work my-project must be configured as a module with its own package.json, see creating NodeJS modules in NPM’s docs for more on this.

Typically, you would specify dependencies in your package.json using package names that can be resolved from the npm registry.

Since npm 2.0, local dependencies are supported natively, as per danilopopeye’s answer on StackOverflow.

There are a few approaches you can use;

1. Use npm install with File Path:

You can use the file path directly in the dependencies section of your package.json.

{
  "name": "my-project",
  "dependencies": {
    "bar": "file:../path/to/my-module"
  }
}

Any of the following paths are also valid:

../path/to/my-module
~/path/to/my-module
./path/to/my-module
/path/to/my-module

2. Use npm link:

If you want to simulate the behavior of a package without publishing it to the npm registry, you can use the npm link command.

  • Navigate to your local module directory: cd /path/to/my-module
  • Run npm link. This will create a global symlink for your local module.
  • Navigate to your project directory: cd /path/to/my-project
  • Run npm link my-module. This will create a symlink in your project’s node_modules that points to your local module.

Now, you can use the local module in your project’s code as if it were a regular npm package.

3. Use Git Repositories:

If your local module is version-controlled using Git, you can specify it as a dependency using a Git URL.

{
  "dependencies": {
    "local-module": "git+file:///path/to/my-module.git"
  }
}

Get latest updates

Since npm install copies my-module into node_modules, changes in my-module‘s source will not automatically be seen by the dependent project.

There are two ways to update the dependent project with

  • Update the version of my-module and then use npm update: As you can see above, the package.json “dependencies” entry does not include a version specifier as you would see for normal dependencies. Instead, for local dependencies, npm update just tries to make sure the latest version is installed, as determined by my-module‘s package.json.
  • Reinstall using npm install. This will install whatever is at my-module‘s source path, even if it is older, or has an alternate branch checked out, whatever.

Always remember to document any unusual dependency management approaches for the benefit of your team and future maintainers.

GET IN TOUCH

Shaun Knowles
Shaun Knowles
Wrote this on
Categrised as:
Development

Taggednpmpackages


More Articles

How to Create a Block Pattern for WordPress

Creating Block Patterns for WordPress: A Beginner’s Guide Want to streamline your website design process? Block Patterns offer pre-designed arrangements of blocks that you can easily insert into your pages and posts. Let’s learn how to create your own. 1. Understand Block Patterns: 2. Set Up Your Development Environment: 3. Create a Block Pattern Directory: […]

Read More… from How to Create a Block Pattern for WordPress

Shaun Knowles
Shaun Knowles
Cover Image for How to Check and Update Your Git Version

How to Check and Update Your Git Version

Change is inevitable and, in the technical world, usually beneficial. Keeping your Git installation up to date is highly recommended, it gives you all the latest features, improvements, bug fixes, and more. Let’s see how to check which Git version you’re currently using and how to update it to the latest version. To stay up […]

Read More… from How to Check and Update Your Git Version

Shaun Knowles
Shaun Knowles