• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [new ArkTS runtime environment](use-napi-ark-runtime.md) created by the **napi_create_ark_runtime** API.
4
5## Function Description
6
7```cpp
8napi_status napi_load_module_with_info(napi_env env, const char* path, const char* module_info, napi_value* result);
9```
10
11| Parameter           | Description         |
12| :------------- | :----------------------------- |
13| env            | Current VM environment.      |
14| path          | Path of the file or name of the module to load.         |
15| module_info   | Path composed of **bundleName** and **moduleName**.      |
16| result         | Module loaded.         |
17
18> **NOTE**
19>
20> - **bundleName** indicates the project name configured in **AppScope/app.json5**.
21> - **moduleName** must be set to the module name configured in the **module.json5** file in the HAP to which the module belongs.
22
23## When to Use
24
25| Scenario           | Description          | Description                        |
26| :------------- | :----------------------------- | :--------------------------- |
27| Local project module  | Load the module file.      | The file path must start with **moduleName**.            |
28| Local project module  | Load the HAR module name.          | -                            |
29| Remote package        | Load the remote HAR module name.       | -                            |
30| Remote package        | Load the ohpm package name.           | -                            |
31| API        |    Load **@ohos.** or **@system.**.         | -                            |
32| Native library  | Load **libNativeLibrary.so**.| -                            |
33
34> **NOTE**
35>
36> - The module name to be loaded is the entry file, generally **index.ets/ts**, of the module.
37> - To load a HAR to another HAR, ensure that **module_info** is correct. The value of **moduleName** must be that of the HAP or HSP.
38> - 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.
39
40## Exception Scenarios
411. The HSP fails to be loaded due to an incorrect module name, and the error code "napi_generic_failure" is returned.
422. 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".
433. The module loading fails due to unexpected behavior on the system side, and **cppcrash** is thrown.
44
45## How to Use
46
47- **Loading a module file**
48
49Load a module from a file, as shown in the following ArkTS code.
50
51```javascript
52//./src/main/ets/Test.ets
53let value = 123;
54function test() {
55  console.log("Hello OpenHarmony");
56}
57export {value, test};
58```
59
601. Configure the **build-profile.json5** file of the current module as follows:
61
62    ```json
63    {
64        "buildOption" : {
65            "arkOptions" : {
66                "runtimeOnly" : {
67                    "sources": [
68                        "./src/main/ets/Test.ets"
69                    ]
70                }
71            }
72        }
73    }
74    ```
75
762. Call **napi_load_module_with_info** to load the **Test.ets** file, call the **test()** function, and obtain the variable values.
77
78    > **NOTE**
79    >
80    > 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**):
81    >
82    > 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**.
83    > 2. The file path must start with **packageName**, which is the value of **name** in the **oh-package.json5** file of the module.
84
85
86    ~~~c++
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 source code HAR module**
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 source code HSP module**
178
179The **Index.ets** file in the HSP is as follows:
180
181```javascript
182//hsp Index.ets
183let value = 123;
184function test() {
185    console.log("Hello World");
186}
187export {value, test};
188```
189
1901. Configure **dependencies** in the **oh-package.json5** file.
191
192    ```json
193    {
194        "dependencies": {
195            "hsp": "file:../hsp"
196        }
197    }
198    ```
199
2002. Configure **build-profile.json5** for the module that uses the HSP.
201
202    ```json
203    {
204        "buildOption" : {
205            "arkOptions" : {
206                "runtimeOnly" : {
207                    "packages": [
208                        "hsp"
209                    ]
210                }
211            }
212        }
213    }
214    ```
215
2163. Call **napi_load_module_with_info** to load the HSP, call the **test** function, and obtain the **value** variable.
217
218    ```cpp
219    static napi_value loadModule(napi_env env, napi_callback_info info) {
220        napi_value result;
221        // 1. Call napi_load_module_with_info to load the HSP.
222        napi_status status = napi_load_module_with_info(env, "hsp", "com.example.application/entry", &result);
223        if (status != napi_ok) {
224            return nullptr;
225        }
226
227        napi_value testFn;
228        // 2. Call napi_get_named_property to obtain the test function.
229        napi_get_named_property(env, result, "test", &testFn);
230        // 3. Call napi_call_function to invoke the test function.
231        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
232
233        napi_value value;
234        napi_value key;
235        std::string keyStr = "value";
236        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
237        // 4. Call napi_get_property to obtain a variable value.
238        napi_get_property(env, result, key, &value);
239        return result;
240    }
241    ```
242
243- **Loading a remote HAR module name**
244
2451. Configure **dependencies** in the **oh-package.json5** file.
246
247    ```json
248    {
249        "dependencies": {
250            "@ohos/hypium": "1.0.16"
251        }
252    }
253    ```
254
2552. Configure **build-profile.json5** for the module that uses @ohos/hypium.
256
257    ```json
258    {
259        "buildOption" : {
260            "arkOptions" : {
261                "runtimeOnly" : {
262                    "packages": [
263                        "@ohos/hypium"
264                    ]
265                }
266            }
267        }
268    }
269    ```
270
2713. Call **napi_load_module_with_info** to load **@ohos/hypium** and obtain the **DEFAULT** variable.
272
273    ```cpp
274    static napi_value loadModule(napi_env env, napi_callback_info info) {
275        napi_value result;
276        // 1. Call napi_load_module_with_info to load @ohos/hypium.
277        napi_status status = napi_load_module_with_info(env, "@ohos/hypium", "com.example.application/entry", &result);
278        if (status != napi_ok) {
279           return nullptr;
280        }
281
282        napi_value key;
283        std::string keyStr = "DEFAULT";
284        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
285        // 2. Call napi_get_property to obtain the DEFAULT variable.
286        napi_value defaultValue;
287        napi_get_property(env, result, key, &defaultValue);
288        return result;
289    }
290    ```
291
292- **Loading an ohpm package name**
293
2941. Configure **dependencies** in the **oh-package.json5** file.
295
296    ```json
297    {
298        "dependencies": {
299            "json5": "^2.2.3"
300        }
301    }
302    ```
303
3042. Configure **build-profile.json5** for the module that uses the .json5 file.
305
306    ```json
307    {
308        "buildOption" : {
309            "arkOptions" : {
310                "runtimeOnly" : {
311                    "packages": [
312                        "json5"
313                    ]
314                }
315            }
316        }
317    }
318    ```
319
3203. Call **napi_load_module_with_info** to load **json5** and call the **stringify** function.
321
322    ```cpp
323    static napi_value loadModule(napi_env env, napi_callback_info info) {
324        napi_value result;
325        // 1. Call napi_load_module_with_info to load json5.
326        napi_status status = napi_load_module_with_info(env, "json5", "com.example.application/entry", &result);
327        if (status != napi_ok) {
328           return nullptr;
329        }
330
331        napi_value key;
332        std::string keyStr = "default";
333        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
334        // 2. Call napi_get_property to obtain the default object.
335        napi_value defaultValue;
336        napi_get_property(env, result, key, &defaultValue);
337
338        napi_value stringifyFn;
339        // 3. Call napi_get_named_property to obtain the stringify function.
340        napi_get_named_property(env, defaultValue, "stringify", &stringifyFn);
341        // 4. Call napi_call_function to invoke the stringify function.
342        napi_value argStr;
343        std::string text = "call json5 stringify";
344        napi_create_string_utf8(env, text.c_str(), text.size(), &argStr);
345        napi_value args[1] = {argStr};
346
347        napi_value returnValue;
348        napi_call_function(env, defaultValue, stringifyFn, 1, args, &returnValue);
349        return result;
350    }
351    ```
352
353- **Loading an API module**
354
355```cpp
356static napi_value loadModule(napi_env env, napi_callback_info info) {
357    // 1. Call napi_load_module_with_info to load the @ohos.hilog module.
358    napi_value result;
359    napi_status status = napi_load_module_with_info(env, "@ohos.hilog", nullptr, &result);
360    if (status != napi_ok) {
361        return nullptr;
362    }
363
364    // 2. Call napi_get_named_property to obtain the info function.
365    napi_value infoFn;
366    napi_get_named_property(env, result, "info", &infoFn);
367
368    napi_value tag;
369    std::string formatStr = "test";
370    napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
371
372    napi_value outputString;
373    std::string str = "Hello OpenHarmony";
374    napi_create_string_utf8(env, str.c_str(), str.size(), &outputString);
375
376    napi_value flag;
377    napi_create_int32(env, 0, &flag);
378
379    napi_value args[3] = {flag, tag, outputString};
380    // 3. Call napi_call_function to invoke the info function.
381    napi_call_function(env, result, infoFn, 3, args, nullptr);
382    return result;
383}
384```
385
386- **Loading a native library**
387
388The **index.d.ts** file of **libentry.so** is as follows:
389
390```javascript
391// index.d.ts
392export const add: (a: number, b: number) => number;
393```
394
3951. Configure **dependencies** in the **oh-package.json5** file.
396
397    ```json
398    {
399        "dependencies": {
400            "libentry.so": "file:../src/main/cpp/types/libentry"
401        }
402    }
403    ```
404
4052. Configure **build-profile.json5** for the module that uses **libentry.so**.
406
407    ```json
408    {
409        "buildOption" : {
410            "arkOptions" : {
411                "runtimeOnly" : {
412                    "packages": [
413                        "libentry.so"
414                    ]
415                }
416            }
417        }
418    }
419    ```
420
4213. Call **napi_load_module_with_info** to load **libentry.so** and call the **add** function.
422
423    ```cpp
424    static constexpr int INT_NUM_2 = 2; // Integer 2
425    static constexpr int INT_NUM_3 = 3; // Integer 3
426
427    static napi_value loadModule(napi_env env, napi_callback_info info) {
428        napi_value result;
429        // 1. Call napi_load_module_with_info to load libentry.so.
430        napi_status status = napi_load_module_with_info(env, "libentry.so", "com.example.application/entry", &result);
431        if (status != napi_ok) {
432            return nullptr;
433        }
434
435        napi_value addFn;
436        // 2. Call napi_get_named_property to obtain the add function.
437        napi_get_named_property(env, result, "add", &addFn);
438
439        napi_value a;
440        napi_value b;
441        napi_create_int32(env, INT_NUM_2, &a);
442        napi_create_int32(env, INT_NUM_3, &b);
443        napi_value args[2] = {a, b};
444        // 3. Call napi_call_function to invoke the add function.
445        napi_value returnValue;
446        napi_call_function(env, result, addFn, INT_NUM_2, args, &returnValue);
447        return result;
448    }
449    ```
450
451- **Loading another HAR module to a HAR**
452
453For example, load **har2** to **har1**. The **Index.ets** file of **har2** is as follows:
454
455```javascript
456//har2 Index.ets
457let value = 123;
458function test() {
459  console.log("Hello OpenHarmony");
460}
461export {value, test};
462```
463
4641. Configure **dependencies** in the **oh-package.json5** file of **har1**.
465
466    ```json
467    {
468        "dependencies": {
469            "har2": "file:../har2"
470        }
471    }
472    ```
473
4742. Configure the **build-profile.json5** file for **har1**.
475
476    ```json
477    {
478        "buildOption" : {
479            "arkOptions" : {
480                "runtimeOnly" : {
481                    "packages": [
482                        "har2"
483                    ]
484                }
485            }
486        }
487    }
488    ```
489
4903. Call **napi_load_module_with_info** to load **har2** to **har1**, call the **test** function, and obtain the **value** variable.
491
492    ```cpp
493    static napi_value loadModule(napi_env env, napi_callback_info info) {
494        napi_value result;
495        // 1. Call napi_load_module_with_info to load har2. Note that moduleName is that of the HAP where the module is located.
496        napi_status status = napi_load_module_with_info(env, "har2", "com.example.application/entry", &result);
497        if (status != napi_ok) {
498            return nullptr;
499        }
500
501        napi_value testFn;
502        // 2. Call napi_get_named_property to obtain the test function.
503        napi_get_named_property(env, result, "test", &testFn);
504        // 3. Call napi_call_function to invoke the test function.
505        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
506
507        napi_value value;
508        napi_value key;
509        std::string keyStr = "value";
510        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
511        // 4. Call napi_get_property to obtain a variable value.
512        napi_get_property(env, result, key, &value);
513        return result;
514    }
515    ```
516