1# Using Third-Party JavaScript and TypeScript Components 2## Overview 3 4OpenHarmony uses a third-party JavaScript or TypeScript component in the form of a Harmony Archive (HAR), which is a static shared package that can contain JavaScript or TypeScript code, C++ libraries, resources, and configuration files. The HAR enables modules and projects to share code related to ArkUI components, resources, and more. Unlike a Harmony Ability Package (HAP), a HAR cannot be independently installed on a device. Instead, it can be referenced only as the dependency of an application module. 5 6## Searching for a Third-Party JavaScript or TypeScript Component 7 81. Access the [OpenHarmony-TPC/tpc_resource](https://gitee.com/openharmony-tpc/tpc_resource) repository, and find the desired component based on the directory index. 9 102. Visit the [OpenHarmony website](https://growing.openharmony.cn/mainPlay/tpc) and search for required third-party components by type, category, or keyword. 11 12 13## Installing and Using a Third-Party JavaScript or TypeScript Component 14 15You can reference a third-party HAR from the ohpm repository or the local repository module. 16 17**Referencing an HAR Installed in the ohpm Repository** 18 19To reference an HAR in the ohpm repository, set the repository information of the third-party HAR. The default repository address on DevEco Studio is "https://repo.harmonyos.com/ohpm/". To use a custom repository address, ensure that the ohpm installation address on DevEco Studio is configured in **Environment Variables > System Variables > PATH**, and then run the following command in the **Terminal** window of DevEco Studio: 20``` 21ohpm config set registry=your_registry1,your_registry2 22``` 23Note: You can configure multiple repository addresses, separated by commas (,). 24Then use either of the following methods to set the third-party HAR as a dependency: 25 - Method 1: In the **Terminal** window, run the following command to install the third-party HAR. DevEco Studio will automatically add the HAR as a dependency to the **oh-package.json5** file of the project. 26 27 ``` 28 ohpm install @ohos/lottie 29 ``` 30 31 - Method 2: Set the third-party HAR as a dependency in the **oh-package.json5** file of the project. The following is a configuration example: 32 33 ``` 34 "dependencies": { "@ohos/lottie": "^2.0.0"} 35 ``` 36 37After the dependency is set, run the **ohpm install** command to install the HAR, which will be stored in the **oh_modules** directory of the project. 38``` 39ohpm install 40``` 41 42**Referencing Files and Resources of the Local Repository Module** 43 44- Method 1: In the **Terminal** window, run the following command to install the third-party HAR. DevEco Studio will automatically add the HAR as a dependency to the **oh-package5.json** file of the project. 45 46 ``` 47 ohpm install ../library 48 ``` 49 50- Method 2: Set the third-party HAR as a dependency in the **oh-package.json5** file of the project. The following is a configuration example: 51 52 ``` 53 "dependencies": { 54 "@ohos/library": "file:../library" 55 } 56 ``` 57 58After the dependency is set, run the **ohpm install** command to install the HAR, which will be stored in the **oh_modules** directory of the project. 59``` 60ohpm install 61``` 62 63> **NOTE** 64> 65> Pay attention to the following points when referencing an HAR: 66>- Only the dependencies declared in **dependencies** of the **oh-package.json5** file in the module and project are used as OpenHarmony dependencies and processed during compilation and building. 67>- The **compileSdkVersion** of the referenced module cannot be earlier than that of the OpenHarmony ohpm third-party package on which the referenced module depends. You can view the version in the **src** > **main** > **module.json5** file of the referenced ohpm package in the **oh_modules** directory. 68 69### Referencing an HAR HML Page 70In a JavaScript project paradigm, component functions are configured in HML files. To reference an HML page in an HAR, first import the page through the **<element>** tag in the HML file of the project. The sample code is as follows: 71``` 72<element name="comp" src="@ohos/library/src/main/js/components/index/index.hml"></element> 73``` 74In the preceding example, **@ohos/library** indicates the name of the HAR, and the path of the HML page is the relative path in the HAR. 75You can then reference the HML page based on the set element name. The sample code is as follows: 76```typescript 77<element name="comp" src="@ohos/library/src/main/js/components/index/index.hml"></element> 78 79<div class="container"> 80 <comp></comp> 81 <text class="title"> 82 {{ $t('strings.hello') }} {{ title }} 83 </text> 84</div> 85``` 86### Referencing an HAR ArkTS Page 87ArkTS is an extension of TypeScript. Therefore, the export and import syntax of ArkTS is the same as that of TypeScript. In the OpenHarmony ohpm module, use **export** to export an ArkTS page. The sample code is as follows: 88```typescript 89// library/src/main/ets/components/MainPage/MainPage.ets 90@Entry 91@Component 92export struct MainPage { 93 @State message: string = 'Hello World' 94 build() { 95 Row() { 96 Column() { 97 Text(this.message) 98 .fontSize(50) 99 .fontWeight(FontWeight.Bold) 100 } 101 .width('100%') 102 } .height('100%') 103 } 104} 105``` 106Import the exported ArkTS page in other modules. The following is an example: 107```typescript 108// entry/MainAbility/pages/index.ets 109 110import { MainPage } from "@ohos/library" 111@Entry 112@Component 113struct Index { 114 @State message: string = 'Hello World' 115 build() { 116 Column() { 117 MainPage() 118 Row() { 119 Text(this.message) 120 .fontSize(50) 121 .fontWeight(FontWeight.Bold) 122 } 123 .width('100%') 124 } 125 .height('10%') 126 } 127} 128``` 129The procedure for referencing JavaScript/TypeScript methods in the HAR is the same as that for referencing ArkTS pages. In the OpenHarmony ohpm module, export the methods through **export**. The sample code is as follows: 130```typescript 131// library/index.js 132export function func() { 133 return "[ohpm] func1"; 134} 135``` 136On other JavaScript/TypeScript pages, use **import** to import the exported methods. The sample code is as follows: 137```typescript 138// entry/src/main/js/MainAbility/pages/index/index.js 139import {func} from "@ohos/library" 140export default { 141 data: { 142 title: "" 143 }, 144 onInit() { 145 this.title = func(); 146 } 147} 148``` 149Resources in an OpenHarmony ohpm module can be referenced in another OpenHarmony ohpm module and modules that depend on the OpenHarmony ohpm module. For example, you can add string resources (defined in **string.json**, **name: hello_ohpm** in this example) and image resources (**icon_ohpm.png** in this example) to **scr/main/resources** of the OpenHarmony ohpm module, and then reference the string and image resources in the entry module, as shown below: 150Currently, referencing resources in **i18n** files is not supported for the JavaScript-based web-like development paradigm. 151```typescript 152// entry/src/main/ets/MainAbility/pages/index.ets 153@Entry 154@Component 155struct Index { 156 @State message: string = 'Hello World' 157 build() { 158 Column() { 159 Row() { 160 Text($r("app.string.hello_ohpm")) // String resource. 161 .fontSize(40) 162 .fontWeight(FontWeight.Bold) 163 } 164 .width('50%') 165 Image($r("app.media.icon_ohpm")) // Image resource. 166 } 167 .height('100%') 168 } 169} 170``` 171During compilation and building of a HAP, DevEco Studio collects resource files from the HAP module and its dependent modules. If the resource files of different modules in the same qualifiers directory have the same name, DevEco Studio overwrites the resource files based on the following priorities (in descending order): 172- AppScope (supported only by the stage model of API version 9) 173- Modules in the HAP file 174- Dependent OpenHarmony ohpm modules 175