Home >Web Front-end >JS Tutorial >Introduction to testing methods of local node packages

Introduction to testing methods of local node packages

不言
不言forward
2018-10-25 13:53:502521browse

This article brings you an introduction to the testing methods of local node packages. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

When we write a node package, we need to test the package locally before publishing it to npm. Suppose we have a local node package xcxutil package that has been written and a project myapp that tests the package in the same folder project. There are the following two ways to test the local xcxutil package in the myapp project

|-- project
    |-- myapp
    |-- xcxutil

Relative path installation xcxutil test

Enter the project directory, find the xcxutil package according to the relative path, and install it directly.

cd myapp
npm install ../xcxutil

Then enter the node_modules folder in myapp to check whether the installation is successful. Generally, as long as the path is correct and the node package is written correctly, the installation will be successful. In this way You can use xcxutil in myapp.

const xcxutil = require('xcxutil');

Note that there must be a package.json file in the myapp directory, otherwise no node package will be installed.

Use npm link to connect to the global test

The above situation is that the node package and the test project are in the same folder, so it is very convenient to find the path, but in many cases it is our local project There are many of them, distributed in different folders, so finding the path is very troublesome, so we recommend using npm link to connect the node package to the global system.

cd xcxutil
npm link

Execute the above command to get the following output

/Users/leinov/.nvm/versions/node/v8.11.3/lib/node_modules/xcxutil -> /Users/leinov/project/xcxutil

means to connect xcxutil under project to the global node_modules Next, we enter the global node_modules package and you can also view the xcxutil package

and then link the package in myapp to test and use it

cd myapp
npm link xcxutil

In this way, xcxutil will be installed under myapp, and the modifications under xcxutil will also be synchronized to myapp, so that local testing can be implemented.

Cancel the global connection

cd xcxutil
npm unlink

xcxutil It has been released, everyone is welcome to use the github address.

The above is the detailed content of Introduction to testing methods of local node packages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete

Related articles

See more