1# Loading a Module in the Main Thread Using Node-API
2
3## **Scenario**
4
5You can use **napi_load_module** to load a module in the main thread. After the 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. Currently, you can use **napi_load_module** to load the following modules:
6
7- System modules, for example, @ohos.hilog
8- Modules defined in the files under the **ets** directory
9
10## Function Description
11
12```cpp
13napi_status napi_load_module(napi_env env,
14                             const char* path,
15                             napi_value* result);
16```
17
18| Parameter           | Description         |
19| :------------- | :----------------------------- |
20| env            | Current VM environment.      |
21| path          | Path of the file or name of the module to load.         |
22| result         | Module loaded.         |
23
24## Constraints
25
26- Do not use this AP in non-main threads.
27- Do not use this API in the **Init()** function.
28- Do not load a file in the callback function of a thread-safe function.
29
30You are advised to use [napi_load_module_with_info](use-napi-load-module-with-info.md) to load modules. This API supports more scenarios.
31
32## Loading a System Module
33
34Use **napi_load_module** to load the system module @ohos.hilog and call the **info** function.
35
36```cpp
37static napi_value loadModule(napi_env env, napi_callback_info info) {
38    // 1. Call napi_load_module to load the @ohos.hilog module.
39    napi_value result;
40    napi_status status = napi_load_module(env, "@ohos.hilog", &result);
41
42    // 2. Call napi_get_named_property to obtain the info function.
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. Call napi_call_function to invoke the info function.
59    napi_call_function(env, result, infoFn, 3, args, nullptr);
60    return result;
61}
62```
63
64## Loading a Module from an ArkTS File
65
66Load a module from a file as shown in the following ArkTS code.
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. Configure the **build-profile.json5** file of the project.
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. Call **napi_load_module** to load the module from the **Test.ets** file, call the **test()** function, and obtain the variable values.
94
95    ```cpp
96    static napi_value loadModule(napi_env env, napi_callback_info info) {
97        napi_value result;
98        // 1. Call napi_load_module to load the module from the Test.ets file.
99        napi_status status = napi_load_module(env, "ets/Test", &result);
100
101        napi_value testFn;
102        // 2. Call napi_get_named_property to obtain the test function.
103        napi_get_named_property(env, result, "test", &testFn);
104        // 3. Call napi_call_function to invoke the test function.
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. Call napi_get_property to obtain a variable value.
112        napi_get_property(env, result, key, &value);
113        return result;
114    }
115    ```
116