1# 使用Node-API接口进行模块加载
2
3Node-API中的napi_load_module_with_info接口的功能是进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数,该函数可以在[新创建的ArkTS基础运行时环境](use-napi-ark-runtime.md)中使用。
4
5## 函数说明
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| 参数            | 说明          |
15| :------------- | :----------------------------- |
16| env            | 当前的虚拟机环境       |
17| path          | 加载的文件路径或者模块名          |
18| module_info   | bundleName/moduleName的路径拼接       |
19| result         | 加载的模块          |
20
21> **注意**
22>
23> 1. bundleName表示AppScope/app.json5中配置的工程名;
24> 2. moduleName指的是待加载模块所在的HAP下module.json5中配置的名字;
25> 3. [napi_load_module](use-napi-load-module.md)只局限于在主线程中进行模块加载。
26
27## napi_load_module_with_info支持的场景
28
29| 场景            | 详细分类           | 说明                         |
30| :------------- | :----------------------------- | :--------------------------- |
31| 本地工程模块   | 加载模块内文件路径       | 要求路径以moduleName开头             |
32| 本地工程模块   | 加载HAR模块名           | -                            |
33| 远程包         | 加载远程HAR模块名        | -                            |
34| 远程包         | 加载ohpm包名            | -                            |
35| API        |    加载@ohos.*或 @system.*          | -                            |
36| 模块Native库   | 加载libNativeLibrary.so | -                            |
37
38> **注意**
39>
40> 1. 加载一个模块名,实际的行为是加载该模块的入口文件,一般为index.ets/ts41> 2. 如果在HAR中加载另外一个HAR,需要确保module_info的配置正确,尤其注意moduleName应为HAP的moduleName。
42> 3. 如果在HAP/HSP中直接或间接使用了三方包,该三方包中使用napi_load_module_with_info接口加载其他模块A,则需要在HAP/HSP中也添加A的依赖。
43
44## 使用示例
45
46- **加载模块内文件路径**
47
48当加载文件中的模块时,如以下ArkTS代码:
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. 需要当前模块的build-profile.json5文件中进行以下配置:
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. 使用napi_load_module_with_info加载Test文件,调用函数test以及获取变量value。
76
77    > **注意**
78    >
79    > 开启useNormalizedOHMUrl后(即将工程目录中与entry同级别的应用级build-profile.json5文件中strictMode属性的useNormalizedOHMUrl字段配置为true),加载模块内文件路径时:1、bundleName不会影响最终加载逻辑,会智能通过module名索引进程内对应的hap,例如:工程的bundleName为com.example.application,实际入参时填写为 com.example.application1,模块也能正常加载。2、路径需要以packageName开头,packageName指的是模块的oh-package.json5中配置的name字段。
80
81    ```cpp
82    static napi_value loadModule(napi_env env, napi_callback_info info) {
83        napi_value result;
84        // 1. 使用napi_load_module_with_info加载Test文件中的模块
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. 使用napi_get_named_property获取test函数
89        napi_get_named_property(env, result, "test", &testFn);
90        // 3. 使用napi_call_function调用函数test
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. 使用napi_get_property获取变量value
98        napi_get_property(env, result, key, &value);
99        return result;
100    }
101    ```
102
103- **加载HAR模块名**
104
105HAR包Index.ets文件如下:
106
107```javascript
108//library Index.ets
109let value = 123;
110function test() {
111  console.log("Hello OpenHarmony");
112}
113export {value, test};
114```
115
1161. 在oh-package.json5文件中配置dependencies项。
117
118    ```json
119    {
120        "dependencies": {
121            "library": "file:../library"
122        }
123    }
124    ```
125
1262. 在使用library的模块中,对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. 用napi_load_module_with_info加载library,调用函数test以及获取变量value。
143
144    ```cpp
145    static napi_value loadModule(napi_env env, napi_callback_info info) {
146        napi_value result;
147        // 1. 使用napi_load_module_with_info加载library
148        napi_status status = napi_load_module_with_info(env, "library", "com.example.application/entry", &result);
149
150        napi_value testFn;
151        // 2. 使用napi_get_named_property获取test函数
152        napi_get_named_property(env, result, "test", &testFn);
153        // 3. 使用napi_call_function调用函数test
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. 使用napi_get_property获取变量value
161        napi_get_property(env, result, key, &value);
162        return result;
163    }
164    ```
165
166- **加载远程HAR模块名**
167
1681. 在oh-package.json5文件中配置dependencies项。
169
170    ```json
171    {
172        "dependencies": {
173            "@ohos/hypium": "1.0.16"
174        }
175    }
176    ```
177
1782. 在使用@ohos/hypium的模块中,对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. 用napi_load_module_with_info加载@ohos/hypium,获取DEFAULT变量。
195
196    ```cpp
197    static napi_value loadModule(napi_env env, napi_callback_info info) {
198        napi_value result;
199        // 1. 使用napi_load_module_with_info加载@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. 使用napi_get_property获取DEFAULT变量
206        napi_value defaultValue;
207        napi_get_property(env, result, key, &defaultValue);
208        return result;
209    }
210    ```
211
212- **加载ohpm包名**
213
2141. 在oh-package.json5文件中配置dependencies项。
215
216    ```json
217    {
218        "dependencies": {
219            "json5": "^2.2.3"
220        }
221    }
222    ```
223
2242. 在使用json5的模块中,对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. 用napi_load_module_with_info加载json5,调用函数stringify。
241
242    ```cpp
243    static napi_value loadModule(napi_env env, napi_callback_info info) {
244        napi_value result;
245        // 1. 使用napi_load_module_with_info加载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. 使用napi_get_property获取default对象
252        napi_value defaultValue;
253        napi_get_property(env, result, key, &defaultValue);
254
255        napi_value stringifyFn;
256        // 3. 使用napi_get_named_property获取stringify函数
257        napi_get_named_property(env, defaultValue, "stringify", &stringifyFn);
258        // 4. 使用napi_call_function调用函数stringify
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- **加载API模块**
271
272```cpp
273static napi_value loadModule(napi_env env, napi_callback_info info) {
274    // 1. 使用napi_load_module_with_info加载模块@ohos.hilog
275    napi_value result;
276    napi_status status = napi_load_module_with_info(env, "@ohos.hilog", nullptr, &result);
277
278    // 2. 使用napi_get_named_property获取info函数
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. 使用napi_call_function调用info函数
295    napi_call_function(env, result, infoFn, 3, args, nullptr);
296    return result;
297}
298```
299
300- **加载Native库**
301
302libentry.soindex.d.ts文件如下
303
304```javascript
305//index.d.ts
306export const add: (a: number, b: number) => number;
307```
308
3091. 在oh-package.json5文件中配置dependencies项。
310
311    ```json
312    {
313        "dependencies": {
314            "libentry.so": "file:../src/main/cpp/types/libentry"
315        }
316    }
317    ```
318
3192. 在使用libentry.so的模块中,对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. 用napi_load_module_with_info加载libentry.so,调用函数add。
336
337    ```cpp
338    static napi_value loadModule(napi_env env, napi_callback_info info) {
339        napi_value result;
340        // 1. 使用napi_load_module_with_info加载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. 使用napi_get_named_property获取add函数
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. 使用napi_call_function调用函数add
353        napi_value returnValue;
354        napi_call_function(env, result, addFn, 2, args, &returnValue);
355        return result;
356    }
357    ```
358
359- **HAR加载HAR模块名**
360
361场景为har1加载har2,har2中的Index.ets文件如下
362
363```javascript
364//har2 Index.ets
365let value = 123;
366function test() {
367  console.log("Hello OpenHarmony");
368}
369export {value, test};
370```
371
3721. 在har1中的oh-package.json5文件中配置dependencies项。
373
374    ```json
375    {
376        "dependencies": {
377            "har2": "file:../har2"
378        }
379    }
380    ```
381
3822. 在har1的build-profile.json5文件中进行配置:
383
384    ```json
385    {
386        "buildOption" : {
387            "arkOptions" : {
388                "runtimeOnly" : {
389                    "packages": [
390                        "har2"
391                    ]
392                }
393            }
394        }
395    }
396    ```
397
3983. 在har1中用napi_load_module_with_info加载har2,调用函数test以及获取变量value。
399
400    ```cpp
401    static napi_value loadModule(napi_env env, napi_callback_info info) {
402        napi_value result;
403        // 1. 使用napi_load_module_with_info加载har2,注意这里的moduleName为模块所在HAP包的moduleName
404        napi_status status = napi_load_module_with_info(env, "har2", "com.example.application/entry", &result);
405
406        napi_value testFn;
407        // 2. 使用napi_get_named_property获取test函数
408        napi_get_named_property(env, result, "test", &testFn);
409        // 3. 使用napi_call_function调用函数test
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. 使用napi_get_property获取变量value
417        napi_get_property(env, result, key, &value);
418        return result;
419    }
420    ```
421