1# Loading a Module Using Node-API 2 3You can use **napi_load_module_with_info** to load a module. After the module is loaded, you can use **napi_get_property** to obtain the variables of the module or use **napi_get_named_property** to obtain the functions of 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** must be set to the module name configured in the **module.json5** file in the HAP to which the module belongs. 25> - You can also use [napi_load_module](use-napi-load-module.md) to load a module. However, **napi_load_module** is limited to loading a module in the main thread only. 26 27## When to Use 28 29| Scenario | Description | Description | 30| :------------- | :----------------------------- | :--------------------------- | 31| Local project module | Load the module file. | The file path must start with **moduleName**. | 32| Local project module | Load the HAR module name. | - | 33| Remote package | Load the remote HAR module name. | - | 34| Remote package | Load the ohpm package name. | - | 35| API | Load **@ohos.** or **@system.**. | - | 36| Native library | Load **libNativeLibrary.so**.| - | 37 38> **NOTE** 39> 40> - The module name 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 has loaded another module, for example, module A, using **napi_load_module_with_info**, you must add module A in the dependencies of the HAP/HSP. 43 44## Exception Scenarios 451. The HSP fails to be loaded, and the error code "napi_generic_failure" is returned. 462. A link error occurs or a file cannot be found in the package during module loading process, and the API throws **referenceError** and returns "napi_pending_exception". 473. The loading fails due to unexpected behavior on the system side, and **cppcrash** is thrown. 48 49## How to Use 50 51- **Loading a module file** 52 53Load a module from a file, as shown in the following ArkTS code. 54 55```javascript 56//./src/main/ets/Test.ets 57let value = 123; 58function test() { 59 console.log("Hello OpenHarmony"); 60} 61export {value, test}; 62``` 63 641. Configure the **build-profile.json5** file of the current module as follows: 65 66 ```json 67 { 68 "buildOption" : { 69 "arkOptions" : { 70 "runtimeOnly" : { 71 "sources": [ 72 "./src/main/ets/Test.ets" 73 ] 74 } 75 } 76 } 77 } 78 ``` 79 802. Call **napi_load_module_with_info** to load the **Test.ets** file, call the **test()** function, and obtain the variable values. 81 82 > **NOTE** 83 > 84 > When a module file is loaded with **useNormalizedOHMUrl** enabled (the **useNormalizedOHMUrl** field of **strictMode** in the application's **build-profile.json5** file in the same directory as **entry** in the project is set to **true**):<br>1. **bundleName** does not affect the loading logic. 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**. <br>2. The file path must start with **packageName**, which is the value of **name** in the **oh-package.json5** file of the module. 85 86 ```cpp 87 static napi_value loadModule(napi_env env, napi_callback_info info) { 88 napi_value result; 89 // 1. Call napi_load_module_with_info to load the module from the Test.ets file. 90 napi_status status = napi_load_module_with_info(env, "entry/src/main/ets/Test", "com.example.application/entry", &result); 91 if (status != napi_ok) { 92 return nullptr; 93 } 94 95 napi_value testFn; 96 // 2. Call napi_get_named_property to obtain the test function. 97 napi_get_named_property(env, result, "test", &testFn); 98 // 3. Call napi_call_function to invoke the test function. 99 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 100 101 napi_value value; 102 napi_value key; 103 std::string keyStr = "value"; 104 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 105 // 4. Call napi_get_property to obtain a variable value. 106 napi_get_property(env, result, key, &value); 107 return result; 108 } 109 ``` 110 111- **Loading a HAR module name** 112 113The **Index.ets** file in the HAR is as follows: 114 115```javascript 116//library Index.ets 117let value = 123; 118function test() { 119 console.log("Hello OpenHarmony"); 120} 121export {value, test}; 122``` 123 1241. Configure **dependencies** in the **oh-package.json5** file. 125 126 ```json 127 { 128 "dependencies": { 129 "library": "file:../library" 130 } 131 } 132 ``` 133 1342. Configure **build-profile.json5** for the module that uses **library**. 135 136 ```json 137 { 138 "buildOption" : { 139 "arkOptions" : { 140 "runtimeOnly" : { 141 "packages": [ 142 "library" 143 ] 144 } 145 } 146 } 147 } 148 ``` 149 1503. Call **napi_load_module_with_info** to load **library**, call the **test** function, and obtain the variable values. 151 152 ```cpp 153 static napi_value loadModule(napi_env env, napi_callback_info info) { 154 napi_value result; 155 // 1. Call napi_load_module_with_info to load library. 156 napi_status status = napi_load_module_with_info(env, "library", "com.example.application/entry", &result); 157 if (status != napi_ok) { 158 return nullptr; 159 } 160 161 napi_value testFn; 162 // 2. Call napi_get_named_property to obtain the test function. 163 napi_get_named_property(env, result, "test", &testFn); 164 // 3. Call napi_call_function to invoke the test function. 165 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 166 167 napi_value value; 168 napi_value key; 169 std::string keyStr = "value"; 170 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 171 // 4. Call napi_get_property to obtain a variable value. 172 napi_get_property(env, result, key, &value); 173 return result; 174 } 175 ``` 176 177- **Loading a remote HAR module name** 178 1791. Configure **dependencies** in the **oh-package.json5** file. 180 181 ```json 182 { 183 "dependencies": { 184 "@ohos/hypium": "1.0.16" 185 } 186 } 187 ``` 188 1892. Configure **build-profile.json5** for the module that uses @ohos/hypium. 190 191 ```json 192 { 193 "buildOption" : { 194 "arkOptions" : { 195 "runtimeOnly" : { 196 "packages": [ 197 "@ohos/hypium" 198 ] 199 } 200 } 201 } 202 } 203 ``` 204 2053. Call **napi_load_module_with_info** to load **@ohos/hypium** and obtain the **DEFAULT** variable. 206 207 ```cpp 208 static napi_value loadModule(napi_env env, napi_callback_info info) { 209 napi_value result; 210 // 1. Call napi_load_module_with_info to load @ohos/hypium. 211 napi_status status = napi_load_module_with_info(env, "@ohos/hypium", "com.example.application/entry", &result); 212 if (status != napi_ok) { 213 return nullptr; 214 } 215 216 napi_value key; 217 std::string keyStr = "DEFAULT"; 218 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 219 // 2. Call napi_get_property to obtain the DEFAULT variable. 220 napi_value defaultValue; 221 napi_get_property(env, result, key, &defaultValue); 222 return result; 223 } 224 ``` 225 226- **Loading an ohpm package name** 227 2281. Configure **dependencies** in the **oh-package.json5** file. 229 230 ```json 231 { 232 "dependencies": { 233 "json5": "^2.2.3" 234 } 235 } 236 ``` 237 2382. Configure **build-profile.json5** for the module that uses the .json5 file. 239 240 ```json 241 { 242 "buildOption" : { 243 "arkOptions" : { 244 "runtimeOnly" : { 245 "packages": [ 246 "json5" 247 ] 248 } 249 } 250 } 251 } 252 ``` 253 2543. Call **napi_load_module_with_info** to load **json5** and call the **stringify** function. 255 256 ```cpp 257 static napi_value loadModule(napi_env env, napi_callback_info info) { 258 napi_value result; 259 // 1. Call napi_load_module_with_info to load json5. 260 napi_status status = napi_load_module_with_info(env, "json5", "com.example.application/entry", &result); 261 if (status != napi_ok) { 262 return nullptr; 263 } 264 265 napi_value key; 266 std::string keyStr = "default"; 267 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 268 // 2. Call napi_get_property to obtain the default object. 269 napi_value defaultValue; 270 napi_get_property(env, result, key, &defaultValue); 271 272 napi_value stringifyFn; 273 // 3. Call napi_get_named_property to obtain the stringify function. 274 napi_get_named_property(env, defaultValue, "stringify", &stringifyFn); 275 // 4. Call napi_call_function to invoke the stringify function. 276 napi_value argStr; 277 std::string text = "call json5 stringify"; 278 napi_create_string_utf8(env, text.c_str(), text.size(), &argStr); 279 napi_value args[1] = {argStr}; 280 281 napi_value returnValue; 282 napi_call_function(env, defaultValue, stringifyFn, 1, args, &returnValue); 283 return result; 284 } 285 ``` 286 287- **Loading an API module** 288 289```cpp 290static napi_value loadModule(napi_env env, napi_callback_info info) { 291 // 1. Call napi_load_module_with_info to load the @ohos.hilog module. 292 napi_value result; 293 napi_status status = napi_load_module_with_info(env, "@ohos.hilog", nullptr, &result); 294 if (status != napi_ok) { 295 return nullptr; 296 } 297 298 // 2. Call napi_get_named_property to obtain the info function. 299 napi_value infoFn; 300 napi_get_named_property(env, result, "info", &infoFn); 301 302 napi_value tag; 303 std::string formatStr = "test"; 304 napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag); 305 306 napi_value outputString; 307 std::string str = "Hello OpenHarmony"; 308 napi_create_string_utf8(env, str.c_str(), str.size(), &outputString); 309 310 napi_value flag; 311 napi_create_int32(env, 0, &flag); 312 313 napi_value args[3] = {flag, tag, outputString}; 314 // 3. Call napi_call_function to invoke the info function. 315 napi_call_function(env, result, infoFn, 3, args, nullptr); 316 return result; 317} 318``` 319 320- **Loading a native library** 321 322The **index.d.ts** file of **libentry.so** is as follows: 323 324```javascript 325// index.d.ts 326export const add: (a: number, b: number) => number; 327``` 328 3291. Configure **dependencies** in the **oh-package.json5** file. 330 331 ```json 332 { 333 "dependencies": { 334 "libentry.so": "file:../src/main/cpp/types/libentry" 335 } 336 } 337 ``` 338 3392. Configure **build-profile.json5** for the module that uses **libentry.so**. 340 341 ```json 342 { 343 "buildOption" : { 344 "arkOptions" : { 345 "runtimeOnly" : { 346 "packages": [ 347 "libentry.so" 348 ] 349 } 350 } 351 } 352 } 353 ``` 354 3553. Call **napi_load_module_with_info** to load **libentry.so** and call the **add** function. 356 357 ```cpp 358 static napi_value loadModule(napi_env env, napi_callback_info info) { 359 napi_value result; 360 // 1. Call napi_load_module_with_info to load libentry.so. 361 napi_status status = napi_load_module_with_info(env, "libentry.so", "com.example.application/entry", &result); 362 if (status != napi_ok) { 363 return nullptr; 364 } 365 366 napi_value addFn; 367 // 2. Call napi_get_named_property to obtain the add function. 368 napi_get_named_property(env, result, "add", &addFn); 369 370 napi_value a; 371 napi_value b; 372 napi_create_int32(env, 2, &a); 373 napi_create_int32(env, 3, &b); 374 napi_value args[2] = {a, b}; 375 // 3. Call napi_call_function to invoke the add function. 376 napi_value returnValue; 377 napi_call_function(env, result, addFn, 2, args, &returnValue); 378 return result; 379 } 380 ``` 381 382- **Loading another HAR module to a HAR** 383 384For example, load **har2** to **har1**. The **Index.ets** file of **har2** is as follows: 385 386```javascript 387//har2 Index.ets 388let value = 123; 389function test() { 390 console.log("Hello OpenHarmony"); 391} 392export {value, test}; 393``` 394 3951. Configure **dependencies** in the **oh-package.json5** file in **har1**. 396 397 ```json 398 { 399 "dependencies": { 400 "har2": "file:../har2" 401 } 402 } 403 ``` 404 4052. Configure the **build-profile.json5** file for **har1**. 406 407 ```json 408 { 409 "buildOption" : { 410 "arkOptions" : { 411 "runtimeOnly" : { 412 "packages": [ 413 "har2" 414 ] 415 } 416 } 417 } 418 } 419 ``` 420 4213. Call **napi_load_module_with_info** to load **har2** to **har1**, call the **test** function, and obtain the variable value. 422 423 ```cpp 424 static napi_value loadModule(napi_env env, napi_callback_info info) { 425 napi_value result; 426 // 1. Call napi_load_module_with_info to load har2. Note that moduleName is that of the HAP where the module is located. 427 napi_status status = napi_load_module_with_info(env, "har2", "com.example.application/entry", &result); 428 if (status != napi_ok) { 429 return nullptr; 430 } 431 432 napi_value testFn; 433 // 2. Call napi_get_named_property to obtain the test function. 434 napi_get_named_property(env, result, "test", &testFn); 435 // 3. Call napi_call_function to invoke the test function. 436 napi_call_function(env, result, testFn, 0, nullptr, nullptr); 437 438 napi_value value; 439 napi_value key; 440 std::string keyStr = "value"; 441 napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key); 442 // 4. Call napi_get_property to obtain a variable value. 443 napi_get_property(env, result, key, &value); 444 return result; 445 } 446 ``` 447