Usage of npm
Introduction
This sample shows how npm references third-party JS class libraries (mathjs and dayjs) and local libraries. The display effect is as follows:

Concepts
npm: a package manager that references third-party JS class libraries and local libraries.
Importing a Third-Party JS Class Library
-
Open the Terminal window and run the following command to go to the entry directory:
cd entry
-
Run the following command to install a third-party library package. In this example, dayjs is being installed.
npm install dayjs --save
-
Import the third-party library package in the corresponding .ets file.
import dayjs from 'dayjs'
Creating a Local Library
-
Right-click the project name, choose New > Module, click Ohos Library on the Choose Your Ability Template page displayed, enter the module name, and click Finish.
-
In the new module directory, edit the name attribute in the Package.json file, for example, name:@ohos/library.
-
In the entry directory, add the new module as a dependency to the dependencies attribute in the Package.json file, for example, "@ohos/library": "../library".
-
In the new Module directory, add the common components or interfaces to be exported to Index.ets file.
-
In the entry directory, import the common component or interface in the new module to be used, for example, import {ThirdTitleBar} from '@ohos/library'.
Required Permissions
N/A
Usage
-
The home page of the sample app shows the application of mathjs. After you touch Click generate verification code, a six-digit random verification code is generated in the lower part. If you touch the button again, a new verification code is displayed. You can touch Back to exit the app.
-
Touch Next page, and the application of dayjs is displayed. Enter the date to be calculated, enter either the number of days backwards or forwards, or both of them, and touch Confirm. The calculation result is displayed in the lower part. You can touch Back to return to the mathjs application page.
-
Touch Next page, and the application of the local library is displayed. Enter two strings as prompted and touch Confirm. The string after concatenation is displayed in the lower part. You can touch Back to return to the dayjs application page.
Constraints
-
This sample can only be run on standard-system devices.
-
In this sample, only open-source pure logic third-party libraries can be imported. The format method in dayjs is not supported.
-
This sample is based on the stage model, which is supported from API version 9.
-
DevEco Studio 3.0 Beta4 (Build version: 3.0.0.992, built on July 14, 2022) must be used.