1# 使用Node-API接口在主线程中进行模块加载
2
3## 场景介绍
4
5Node-API中的napi_load_module接口的功能是在主线程中进行模块的加载,当模块加载出来之后,可以使用函数napi_get_property获取模块导出的变量,也可以使用napi_get_named_property获取模块导出的函数,目前支持以下场景:
6
7- 加载系统模块,例如@ohos.hilog
8- 加载ets目录下文件中的模块
9
10## 函数说明
11
12```cpp
13napi_status napi_load_module(napi_env env,
14                             const char* path,
15                             napi_value* result);
16```
17
18| 参数            | 说明          |
19| :------------- | :----------------------------- |
20| env            | 当前的虚拟机环境       |
21| path          | 加载的文件路径或者模块名          |
22| result         | 加载的模块          |
23
24## 使用限制
25
26- 禁止在非主线程当中使用该接口。
27- 禁止在Init函数中使用该接口。
28- 禁止在线程安全函数的回调函数当中进行文件路径的加载。
29
30建议使用[napi_load_module_with_info](use-napi-load-module-with-info.md)来进行模块加载,该接口支持了更多的场景。
31
32## 加载系统模块使用示例
33
34使用napi_load_module导出系统模块hilog,并调用info函数。
35
36```cpp
37static napi_value loadModule(napi_env env, napi_callback_info info) {
38    // 1. 使用napi_load_module加载模块@ohos.hilog
39    napi_value result;
40    napi_status status = napi_load_module(env, "@ohos.hilog", &result);
41
42    // 2. 使用napi_get_named_property获取info函数
43    napi_value infoFn;
44    napi_get_named_property(env, result, "info", &infoFn);
45
46    napi_value tag;
47    std::string formatStr = "test";
48    napi_create_string_utf8(env, formatStr.c_str(), formatStr.size(), &tag);
49
50    napi_value outputString;
51    std::string str = "Hello OpenHarmony";
52    napi_create_string_utf8(env, str.c_str(), str.size(), &outputString);
53
54    napi_value flag;
55    napi_create_int32(env, 0, &flag);
56
57    napi_value args[3] = {flag, tag, outputString};
58    // 3. 使用napi_call_function调用info函数
59    napi_call_function(env, result, infoFn, 3, args, nullptr);
60    return result;
61}
62```
63
64## 加载ArkTS文件中的模块使用示例
65
66当加载文件中的模块时,如以下ArkTS代码:
67
68```javascript
69//./src/main/ets/Test.ets
70let value = 123;
71function test() {
72  console.log("Hello OpenHarmony");
73}
74export {value, test};
75```
76
771. 需要在工程的build-profile.json5文件中进行以下配置:
78
79    ```json
80    {
81        "buildOption" : {
82            "arkOptions" : {
83                "runtimeOnly" : {
84                    "sources": [
85                        "./src/main/ets/Test.ets"
86                    ]
87                }
88            }
89        }
90    }
91    ```
92
932. 使用napi_load_module加载Test文件,调用函数test以及获取变量value。
94
95    ```cpp
96    static napi_value loadModule(napi_env env, napi_callback_info info) {
97        napi_value result;
98        // 1. 使用napi_load_module加载Test文件中的模块
99        napi_status status = napi_load_module(env, "ets/Test", &result);
100
101        napi_value testFn;
102        // 2. 使用napi_get_named_property获取test函数
103        napi_get_named_property(env, result, "test", &testFn);
104        // 3. 使用napi_call_function调用函数test
105        napi_call_function(env, result, testFn, 0, nullptr, nullptr);
106
107        napi_value value;
108        napi_value key;
109        std::string keyStr = "value";
110        napi_create_string_utf8(env, keyStr.c_str(), keyStr.size(), &key);
111        // 4. 使用napi_get_property获取变量value
112        napi_get_property(env, result, key, &value);
113        return result;
114    }
115    ```
116