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. Access the [OpenHarmony Third-Party Libraries](https://ohpm.openharmony.cn/#/en/home) website, and search for the required third-party component. 11 12## Installing and Using a Third-Party JavaScript or TypeScript Component 13 14You can reference a third-party HAR from the ohpm repository or the local repository module. 15 16**Referencing an HAR Installed in the ohpm Repository** 17 18To 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: 19``` 20ohpm config set registry=your_registry1,your_registry2 21``` 22Note: You can configure multiple repository addresses, separated by commas (,). 23Then use either of the following methods to set the third-party HAR as a dependency: 24 - 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. 25 26 ``` 27 ohpm install @ohos/lottie 28 ``` 29 30 - 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: 31 32 ``` 33 "dependencies": { "@ohos/lottie": "^2.0.0"} 34 ``` 35 36After 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. 37``` 38ohpm install 39``` 40 41**Referencing Files and Resources of the Local Repository Module** 42 43- 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. 44 45 ``` 46 ohpm install ../library 47 ``` 48 49- 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: 50 51 ``` 52 "dependencies": { 53 "library": "file:../library" 54 } 55 ``` 56 57After 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. 58``` 59ohpm install 60``` 61 62> **NOTE** 63> 64> Pay attention to the following points when referencing an HAR: 65>- 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. 66>- 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. 67 68### Referencing an HAR HML Page 69In 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: 70``` 71<element name="comp" src="library/src/main/js/components/index/index.hml"></element> 72``` 73In the preceding example, **library** indicates the name of the HAR, and the path of the HML page is the relative path in the HAR. 74You can then reference the HML page based on the set element name. The sample code is as follows: 75```typescript 76<element name="comp" src="library/src/main/js/components/index/index.hml"></element> 77 78<div class="container"> 79 <comp></comp> 80 <text class="title"> 81 {{ $t('strings.hello') }} {{ title }} 82 </text> 83</div> 84``` 85### Referencing an HAR ArkTS Page 86ArkTS 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: 87```typescript 88// library/src/main/ets/components/mainpage/MainPage.ets 89@Entry 90@Component 91export struct MainPage { 92 @State message: string = 'Hello World' 93 build() { 94 Row() { 95 Column() { 96 Text(this.message) 97 .fontSize(50) 98 .fontWeight(FontWeight.Bold) 99 } 100 .width('100%') 101 } .height('100%') 102 } 103} 104``` 105Import the exported ArkTS page in other modules. The following is an example: 106```typescript 107// entry/MainAbility/pages/index.ets 108 109import { MainPage } from "library" 110@Entry 111@Component 112struct Index { 113 @State message: string = 'Hello World' 114 build() { 115 Column() { 116 MainPage() 117 Row() { 118 Text(this.message) 119 .fontSize(50) 120 .fontWeight(FontWeight.Bold) 121 } 122 .width('100%') 123 } 124 .height('10%') 125 } 126} 127``` 128The 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: 129```typescript 130// library/index.js 131export function func() { 132 return "[ohpm] func1"; 133} 134``` 135On other JavaScript/TypeScript pages, use **import** to import the exported methods. The sample code is as follows: 136```typescript 137// entry/src/main/js/MainAbility/pages/index/index.js 138import {func} from "library" 139export default { 140 data: { 141 title: "" 142 }, 143 onInit() { 144 this.title = func(); 145 } 146} 147``` 148Resources 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: 149Currently, referencing resources in **i18n** files is not supported for the JavaScript-based web-like development paradigm. 150```typescript 151// entry/src/main/ets/MainAbility/pages/index.ets 152@Entry 153@Component 154struct Index { 155 @State message: string = 'Hello World' 156 build() { 157 Column() { 158 Row() { 159 Text($r("app.string.hello_ohpm")) // String resource. 160 .fontSize(40) 161 .fontWeight(FontWeight.Bold) 162 } 163 .width('50%') 164 Image($r("app.media.icon_ohpm")) // Image resource. 165 } 166 .height('100%') 167 } 168} 169``` 170During 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): 171- AppScope (supported only by the stage model of API version 9) 172- Modules in the HAP file 173- Dependent OpenHarmony ohpm modules 174