This is the first day of my participation in the August Text Challenge.More challenges in August

Lynne, a front-end development engineer who can cry, love and laugh forever. In the Internet wave, love life and technology.

preface

Node application development, it is inevitable to use or split into NPM modules.

In the process of developing NPM package, debugging code can be easily realized with the help of NPM Link. This article mainly introduces 5 ways of NPM Link, the first two are the mainstream ways. A look will be ~ ~

Method 1 link module project path

$ cd path/to/my-project // Cut to the import project path first

$ npm link path/to/my-utils // Link is then introduced into the project path of the NPM package
Copy the code

The above method applies when referencing and importing projects in the same directory.

Removing link is easy as long as:

$ npm unlink path/to/my-utils
Copy the code

Method 2 link module project name

$# Go to the module directory, Link $CD path/to/my-utils $NPM link $$# Link $CD path/to/my-utils $NPM link my-utilsCopy the code

This should be the most popular approach for developers!

I often use this approach in my projects:

npm link

npm link XXX

Removing link is easy as long as:

$ npm unlink my-utils
Copy the code

Wraning appears in the figure, which is obviously a different package and does not care.

Method three: Use a soft chain

$ cd path/to/my-project/node_modules
$ ln -s path/to/my-utils my-utils
Copy the code

You can use NPM config get prefix to view path prefixes.

Disadvantages: troublesome instruction operation, different operating system syntax is different

But the same operating system is still very fragrant ~~~~

Here are two equally possible but less useful counterexamples:

Beta release

There seems to be no difference except that it does not occupy the official version, too much trouble!

Install directly using relative paths

$ cd path/to/my-project
$ npm install path/to/my-utils
Copy the code

The downside is that switching paths is cumbersome.

release

Debug and publish

NPM install uses Latest to set the default installed version, alpha to set the test version, and alpha to set the test version when the test version is completed

npm dist-tag add <pkg>@<version> [<tag>]
Copy the code

For example, if 1.3.1 has been tested, we can use NPM dist-tag add [email protected] latest to cut the branch from alpha to latest. “Latest” is default if tag is not added.

Of course, you can also add your own tag via MPM dist- Tag Add.

Added: Fix bugs

Does anyone have bugs with such a simple operation? It’s me…

NPM link error: not found ‘moudule’…

Two troubleshooting ideas, according to the error report to judge and check the installation of packages not found:

  1. NPM install does not install NPM install…

  2. Whether the update package dependencies need to be installed locally

First of all, we need to know the principle of NPM link, which is implemented by soft chain. The local package of LINK will completely replace the original package in our project node_moudules, and may delete some dependencies in the project. Therefore, we need to install these mistakenly deleted dependencies in the project separately.

conclusion

Have you learned how to debug such a simple module?

Package. json:

  • Package. json: Used to annotate dependencies on individual NPM packages in a project.
  • Package-lock. json: records the source and version of each NPM package actually installed in the current state.