@ Subject: test the local node package
@ Author: leinov
@ Date: 2018-10-25
When writing a node package, we need to test the package locally before releasing it to NPM. Suppose we have a locally written node package.xcxutil
Package and a project to test the packagemyapp
In the same folderproject
The following two methods are available:myapp
Test localxcxutil
Package
|-- project |-- myapp |-- xcxutil
Install xcxutil in the relative path
Go to the project directory, find the xcxutil package in the corresponding path, and install it directly.
cd myappnpm install ../xcxutil
Then entermyapp
Innode_modules
Check whether the folder is successfully installed. Generally, if the path is correct and the node package is correctly written, the installation is successful, so that you can usexcxutil
.
const xcxutil = require('xcxutil');
Note,myapp
The directory must havepackage.json
File. Otherwise, no node package can be installed.
Connect to global test with NPM Link
The above situation is that the node package and test project are in the same folder, so it is very convenient to find the path, but in many cases our local projects are distributed in different folders, so it is very troublesome to find the path, so we recommend that you usenpm link
This method connects the node package to the global environment.
cd xcxutilnpm link
Run the preceding command to obtain the following output:
/Users/leinov/.nvm/versions/node/v8.11.3/lib/node_modules/xcxutil -> /Users/leinov/project/xcxutil
It meansproject
Underxcxutil
Connect to the globalnode_modules
Next, we enter the globalnode_modules
You can also view the packagexcxutil
Package
Then link the package in MyApp to test the usage.
cd myappnpm link xcxutil
In this way, xcxutil will be installed under MyApp, and modifications made under xcxutil will also be synchronized to MyApp to implement local testing,
Cancel global connection
cd xcxutilnpm unlink
Xcxutil has been released. Welcome to GitHub.
Original article address: 1190000016799775
Test local node package