Home > Web Front-end > JS Tutorial > body text

Introduction to testing methods of local node packages

不言
Release: 2018-10-25 13:53:50
forward
2474 people have browsed it

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
Copy after login

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
Copy after login

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');
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!