1# Loading a Module Using Node-API 2 3You can use **napi_load_module_with_info** to load modules. After a module is loaded, you can use **napi_get_property** to obtain the variables exported by the module or use **napi_get_named_property** to obtain the functions exported by the module. The **napi_load_module_with_info** API can be used in a [newly created ArkTS runtime environment](use-napi-ark-runtime.md). 4 5## Function Description 6 7```cpp 8napi_status napi_load_module_with_info(napi_env env, 9 const char* path, 10 const char* module_info, 11 napi_value* result); 12``` 13 14| Parameter | Description | 15| :------------- | :----------------------------- | 16| env | Current VM environment. | 17| path | Path of the file or name of the module to load. | 18| module_info | Path composed of **bundleName** and **moduleName**. | 19| result | Module loaded. | 20 21> **NOTE** 22> 23> - **bundleName** indicates the project name configured in **AppScope/app.json5**. 24> - **moduleName** indicates the module name configured in **module.json5** in the HAP of the module to be loaded. 25> - You can also use [napi_load_module](use-napi-load-module.md) to load modules. However, **napi_load_module** is limited to loading modules in the main thread only. 26 27## When to Use 28 29| Scenario | Description | Description | 30| :------------- | :----------------------------- | :--------------------------- | 31| Local project module | Load a module from a local project file using the relative path to a HAP. | The file paths must start with **moduleName**. | 32| Local HAR module | Load a HAR module to a HAP. | - | 33| Remote HAR module | Load a remote HAR module to a HAP. | - | 34| Remote ohpm module | Load an ohpm package to a HAP. | - | 35| API | Load @ohos. or @system. to a HAP. | - | 36| Native library module | Load **libNativeLibrary.so** to a HAP.| - | 37 38> **NOTE** 39> 40> - The "module" to be loaded is the entry file, generally **index.ets/ts**, of the module. 41> - To load a HAR to another HAR, ensure that **module_info** is correct. The value of **moduleName** must be that of the HAP. 42> - If a third-party package is directly or indirectly used in a HAP/HSP and the third-party package uses **napi_load_module_with_info** to load other module, for example, module A, the dependency of module A must also be added to the HAP/HSP. 43 44## How to Use 45 46- **Loading a module from a local project file to a HAP** 47 48Load a module from a file as shown in the following ArkTS code. 49 50```javascript 51//./src/main/ets/Test.ets 52let value = 123; 53function test() { 54 console.log("Hello OpenHarmony"); 55} 56export {value, test}; 57``` 58 591. Add the following to the **build-profile.json5** file of the project. 60 61```json 62{ 63 "buildOption" : { 64 "arkOptions" : { 65 "runtimeOnly" : { 66 "sources": [ 67 "./src/main/ets/Test.ets" 68 ] 69 } 70 } 71 } 72} 73``` 74 752. Use **napi_load_module_with_info** to load the **Test.ets** file, call the **test()** function, and obtain the variable values. 76 77> **NOTE** 78> 79> If **seNormalizedOHMUrl** is enabled (the **useNormalizedOHMUrl** field of **strictMode** in the application's **build-profile.json5** file at the same level as **entry** in the project directory is set to **true**), **bundleName** does not affect the loading logic when a HAP module is loaded. The corresponding HAP in the process is intelligently indexed based on the module name. For example, the module can be successfully loaded if **bundleName** is set to **com.example.application1** while the actual bundle name of the project is **com.example.application**. 80 81```cpp 82static napi_value loadModule(napi_env env, napi_callback_info info) { 83 napi_value result; 84 // 1. Call napi_load_module_with_info to load the module from the Test.ets file. 85 napi_status status = napi_load_module_with_info(env, "entry/src/main/ets/Test", "com.example.application/entry", &result); 86 87 napi_value testFn; 88 // 2. Call napi_get_named_property to obtain the test function. 89 napi_get_named_property(env, result, "test", &testFn); 90 // 3. Call napi_call_function to call the test function. 91 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 92 93 napi_value value; 94 napi_value key; 95 std::string keyStr = "value"; 96 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 97 // 4. Call napi_get_property to obtain a variable value. 98 napi_get_property(env, result, key, &value); 99 return result; 100} 101``` 102 103- **Loading a HAR module to a HAP** 104 105The **Index.ets** file in the HAR package is as follows: 106 107```javascript 108//library Index.ets 109let value = 123; 110function test() { 111 console.log("Hello OpenHarmony"); 112} 113export {value, test}; 114``` 115 1161. Configure dependencies in the **oh-package.json5** file. 117 118```json 119{ 120 "dependencies": { 121 "library": "file:../library" 122 } 123} 124``` 125 1262. Configure the source package in **build-profile.json5**. 127 128```json 129{ 130 "buildOption" : { 131 "arkOptions" : { 132 "runtimeOnly" : { 133 "packages": [ 134 "library" 135 ] 136 } 137 } 138 } 139} 140``` 141 1423. Use **napi_load_module_with_info** to load **library**, call the **test** function, and obtain the variable values. 143 144```cpp 145static napi_value loadModule(napi_env env, napi_callback_info info) { 146 napi_value result; 147 // 1. Call napi_load_module_with_info to load library. 148 napi_status status = napi_load_module_with_info(env, "library", "com.example.application/entry", &result); 149 150 napi_value testFn; 151 // 2. Call napi_get_named_property to obtain the test function. 152 napi_get_named_property(env, result, "test", &testFn); 153 // 3. Call napi_call_function to call the test function. 154 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 155 156 napi_value value; 157 napi_value key; 158 std::string keyStr = "value"; 159 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 160 // 4. Call napi_get_property to obtain a variable value. 161 napi_get_property(env, result, key, &value); 162 return result; 163} 164``` 165 166- **Loading a remote HAR module to a HAP** 167 1681. Configure dependencies in the **oh-package.json5** file. 169 170```json 171{ 172 "dependencies": { 173 "@ohos/hypium": "1.0.16" 174 } 175} 176``` 177 1782. Configure the source package in **build-profile.json5**. 179 180```json 181{ 182 "buildOption" : { 183 "arkOptions" : { 184 "runtimeOnly" : { 185 "packages": [ 186 "@ohos/hypium" 187 ] 188 } 189 } 190 } 191} 192``` 193 1943. Use **napi_load_module_with_info** to load **@ohos/hypium** and obtain the default variable. 195 196```cpp 197static napi_value loadModule(napi_env env, napi_callback_info info) { 198 napi_value result; 199 // 1. Call napi_load_module_with_info to load @ohos/hypium. 200 napi_status status = napi_load_module_with_info(env, "@ohos/hypium", "com.example.application/entry", &result); 201 202 napi_value key; 203 std::string keyStr = "DEFAULT"; 204 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 205 // 2. Call napi_get_property to obtain the default variable. 206 napi_value defaultValue; 207 napi_get_property(env, result, key, &defaultValue); 208 return result; 209} 210``` 211 212- **Loading an ohpm package to a HAP** 213 2141. Configure dependencies in the **oh-package.json5** file. 215 216```json 217{ 218 "dependencies": { 219 "json5": "^2.2.3" 220 } 221} 222``` 223 2242. Configure the source package in **build-profile.json5**. 225 226```json 227{ 228 "buildOption" : { 229 "arkOptions" : { 230 "runtimeOnly" : { 231 "packages": [ 232 "json5" 233 ] 234 } 235 } 236 } 237} 238``` 239 2403. Use **napi_load_module_with_info** to load **json5** and call the **stringify** function. 241 242```cpp 243static napi_value loadModule(napi_env env, napi_callback_info info) { 244 napi_value result; 245 // 1. Call napi_load_module_with_info to load json5. 246 napi_status status = napi_load_module_with_info(env, "json5", "com.example.application/entry", &result); 247 248 napi_value key; 249 std::string keyStr = "default"; 250 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 251 // 2. Call napi_get_property to obtain the default object. 252 napi_value defaultValue; 253 napi_get_property(env, result, key, &defaultValue); 254 255 napi_value stringifyFn; 256 // 3. Call napi_get_named_property to obtain the stringify function. 257 napi_get_named_property(env, defaultValue, "stringify", &stringifyFn); 258 // 4. Use napi_call_function to call the stringify function. 259 napi_value argStr; 260 std::string text = "call json5 stringify"; 261 napi_create_string_utf8(env, text.c_str(), text.size(), &argStr); 262 napi_value args[1] = {argStr}; 263 264 napi_value returnValue; 265 napi_call_function(env, defaultValue, stringifyFn, 1, args, &returnValue); 266 return result; 267} 268``` 269 270- **Loading an API module to a HAP** 271 272```cpp 273static napi_value loadModule(napi_env env, napi_callback_info info) { 274 // 1. Call napi_load_module_with_info to load the module @ohos.hilog. 275 napi_value result; 276 napi_status status = napi_load_module_with_info(env, "@ohos.hilog", nullptr, &result); 277 278 // 2. Call napi_get_named_property to obtain the info function. 279 napi_value infoFn; 280 napi_get_named_property(env, result, "info", &infoFn); 281 282 napi_value tag; 283 std::string formatStr = "test"; 284 napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag); 285 286 napi_value outputString; 287 std::string str = "Hello OpenHarmony"; 288 napi_create_string_utf8(env, str.c_str(), str.size(), &outputString); 289 290 napi_value flag; 291 napi_create_int32(env, 0, &flag); 292 293 napi_value args[3] = {flag, tag, outputString}; 294 // 3. Use napi_call_function to call the info function. 295 napi_call_function(env, result, infoFn, 3, args, nullptr); 296 return result; 297} 298``` 299 300- **Loading a native library to a HAP** 301 302The **index.d.ts** file of **libentry.so** is as follows: 303 304```javascript 305// index.d.ts 306export const add: (a: number, b: number) => number; 307``` 308 3091. Configure dependencies in the **oh-package.json5** file. 310 311```json 312{ 313 "dependencies": { 314 "libentry.so": "file:../src/main/cpp/types/libentry" 315 } 316} 317``` 318 3192. Configure the source package in **build-profile.json5**. 320 321```json 322{ 323 "buildOption" : { 324 "arkOptions" : { 325 "runtimeOnly" : { 326 "packages": [ 327 "libentry.so" 328 ] 329 } 330 } 331 } 332} 333``` 334 3353. Use **napi_load_module_with_info** to load **libentry.so** and call the **add** function. 336 337```cpp 338static napi_value loadModule(napi_env env, napi_callback_info info) { 339 napi_value result; 340 // 1. Call napi_load_module_with_info to load libentry.so. 341 napi_status status = napi_load_module_with_info(env, "libentry.so", "com.example.application/entry", &result); 342 343 napi_value addFn; 344 // 2. Call napi_get_named_property to obtain the add function. 345 napi_get_named_property(env, result, "add", &addFn); 346 347 napi_value a; 348 napi_value b; 349 napi_create_int32(env, 2, &a); 350 napi_create_int32(env, 3, &b); 351 napi_value args[2] = {a, b}; 352 // 3. Use napi_call_function to call the add function. 353 napi_value returnValue; 354 napi_call_function(env, result, addFn, 2, args, &returnValue); 355 return result; 356} 357``` 358 359- **Loading a HAR module to a HAR** 360 361Load **har2** to **har1**. The **Index.ets** file of **har2** is as follows: 362 363```javascript 364// har2 Index.ets 365let value = 123; 366function test() { 367 console.log("Hello OpenHarmony"); 368} 369export {value, test}; 370``` 371 3721. Configure **dependencies** in the **oh-package.json5** file of **har1**. 373 374```json 375{ 376 "dependencies": { 377 "har2": "file:../har2" 378 } 379} 380``` 381 3822. Configure the source package in the **build-profile.json5** file of **har1**. 383 384```json 385{ 386 "buildOption" : { 387 "arkOptions" : { 388 "runtimeOnly" : { 389 "packages": [ 390 "har2" 391 ] 392 } 393 } 394 } 395} 396``` 397 3983. In **har1**, use **napi_load_module_with_info** to load **har2**, call the **test** function, and obtain the variable value. 399 400```cpp 401static napi_value loadModule(napi_env env, napi_callback_info info) { 402 napi_value result; 403 // 1. Call napi_load_module_with_info to load har2. Note that moduleName is that of the HAP where the module is located. 404 napi_status status = napi_load_module_with_info(env, "har2", "com.example.application/entry", &result); 405 406 napi_value testFn; 407 // 2. Call napi_get_named_property to obtain the test function. 408 napi_get_named_property(env, result, "test", &testFn); 409 // 3. Call napi_call_function to call the test function. 410 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 411 412 napi_value value; 413 napi_value key; 414 std::string keyStr = "value"; 415 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 416 // 4. Call napi_get_property to obtain a variable value. 417 napi_get_property(env, result, key, &value); 418 return result; 419} 420``` 421<!--no_check-->