1. The background
In node application development, we inevitably need to use or split into NPM modules. One problem we often encounter is:
How will a newly developed or modified NPM module be tested in a project?
New students generally have the following ways:
For demonstration purposes, let’s assume that the project is my-project and needs a separate my-utils module
1.1 Release a beta
- Advantage: you are happy.
- Cons: boring + boring + boring, trouble + trouble + trouble.
1.2 Install using relative paths
$ cd path/to/my-project
$ npm install path/to/my-utilsCopy the code
- Pros: Simplicity and clarity
- Disadvantages: Tuning is often required during debugging, which can be cumbersome by switching to the my-utils directory for modifications and then repeatedly reinstalling
1.3 Use a soft chain
$ cd path/to/my-project/node_modules
$ ln -s path/to/my-utils my-utilsCopy the code
- Advantages: soft chain after modification on both sides of the direct synchronization
- Disadvantages: troublesome instruction operation, different operating system syntax is different
2. Correct solution – NPM link
But NPM itself already provides a special NPM link directive for such cases.
Related documents: docs.npmjs.com/cli/link
Here is a brief introduction to usage:
$ cd path/to/my-project
$ npm link path/to/my-utilsCopy the code
Simply replace one word and you’re done. Cool
If the two directories are not together, there is an alternative:
$# 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 directive can also be used to debug the Node CLI module, for example to debug our egg-init locally:
$CD path/to/egg-init $NPM link $#Copy the code
It is also easy to remove link:
$ npm unlink my-utilsCopy the code
3. Write at the end
- This method is only for the last step of debugging, the correctness of the module itself, should be more through the unit test to ensure.
- For details about unit testing, see Unit Testing