Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop npm module locally

Lets say im working on an app, MyApp, and I want to build an NPM module for it, MyModule. Right now I can think of two ways to develop it:

  1. Makes changes -> save -> npm install /path/to/module in MyApp
  2. Same as 1, except run npm install /path/to/module then editing it directly in node_modules then copying the changes over.

What I'd like is an easier workflow. One where I can simply save the file, refresh the page, and my changes are there. Is that possible? For example, I know in Gemfiles I can just link to another directory as the path. Pretty sure I can't do that with npm tho.

like image 540
Oscar Godson Avatar asked Jan 02 '14 17:01

Oscar Godson


People also ask

How use NPM package locally?

With your NPM package local to your machine, you'll need a way to reference/install it in the test application. Inside of the original NPM package directory, run npm link from the command line. This command will allow us to simulate installing this NPM package without it actually being published.

Can I install npm locally?

You can install a package locally if you want to depend on the package from your own module, using something like Node. js require . This is npm install 's default behavior.

What is the npm syntax to install packages locally?

Installing a package with dist-tags To override this behavior, use the command below (where tag is the package's tag like beta , latest , etc.). npm install <package_name>@<tag>. To install a specific version of the ExpressJS library, type: npm install express@beta.


1 Answers

You're looking for the npm link command, which is a two steps process:

  1. Run npm link from your MyModule directory: this will create a global package symlinked to the MyModule directory
  2. Run npm link MyModule from your MyApp directory: this will create a MyModule folder in node_modules, symlinked to the global symlink (and thus to the real location of MyModule).
like image 84
Paul Mougel Avatar answered Sep 19 '22 11:09

Paul Mougel