1# Working with Other Node-API Utilities
2
3## Introduction
4
5Node-API also provides some useful APIs to improve development experience.
6
7## Basic Concepts
8
9- Module loading: A module is an ArkTS file that contains specific functionalities. You can import a module to the shared library. Understanding the loading mechanism and dependencies between Node-API modules is helpful for using **node_api_get_module_file_name** correctly.
10- File path and URL: The return value of **node_api_get_module_file_name** is the absolute path of the module to be loaded.
11- Strict equality check: The strict equality check is used to check whether two ArkTS values are equal in type and value. When type conversion is considered, if the values being compared are of different types, **false** will be returned even if the values are the same.
12- Asynchronous operation processing: libuv can be used to implement asynchronous operations to avoid blocking the main thread.
13- Event loop: The Node-API module leverages libuv to implement the event-driven programming model. libuv provides an event loop mechanism to process events, trigger callbacks, and manage event queues.
14
15## Available APIs
16
17| API| Description|
18| -------- | -------- |
19| node_api_get_module_file_name | Obtains the absolute path of the module to be loaded.|
20| napi_strict_equals | Checks whether two values are strictly equal, that is, equal in both the value and type. For example, you can use **napi_strict_equals** to ensure data consistency when working on data structs or algorithms of the specific type. |
21
22## Example
23
24If you are just starting out with Node-API, see [Node-API Development Process](use-napi-process.md). The following demonstrates only the C++ and ArkTS code involved in the APIs mentioned in this topic.
25
26### node_api_get_module_file_name
27
28Use **node_api_get_module_file_name** to obtain the absolute path of the module to be loaded.
29
30CPP code:
31
32```cpp
33#include "napi/native_api.h"
34
35static napi_value GetModuleFileName(napi_env env, napi_callback_info info)
36{
37    // Declare file, a pointer variable of the const char type, to store the absolute path of the module.
38    const char *file = nullptr;
39    napi_value value = nullptr;
40    // Obtain the absolute path of the module and store it in the file variable.
41    napi_status status = node_api_get_module_file_name(env, &file);
42    if (status != napi_ok) {
43        napi_throw_error(env, nullptr, "Failed to get module file name");
44        return nullptr;
45    }
46    // Create a string of the napi_value type that contains the absolute path.
47    napi_create_string_utf8(env, file, NAPI_AUTO_LENGTH, &value);
48    return value;
49}
50```
51
52API declaration:
53
54```ts
55// index.d.ts
56export const getModuleFileName: () => string | void;
57```
58
59ArkTS code:
60
61```ts
62import hilog from '@ohos.hilog'
63import testNapi from 'libentry.so'
64
65let filename = testNapi.getModuleFileName();
66hilog.info(0x0000, 'testTag', 'Test Node-API node_api_get_module_file_name:%{public}s', filename);
67```
68
69### napi_strict_equals
70
71Use **napi_strict_equals** to check whether two ArkTS values are strictly equal.
72
73CPP code:
74
75```cpp
76#include "napi/native_api.h"
77
78static napi_value StrictEquals(napi_env env, napi_callback_info info)
79{
80    // Obtain the two parameters passed from ArkTS.
81    size_t argc = 2;
82    napi_value args[2] = {nullptr};
83    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
84    // Call napi_strict_equals to check whether two ArkTS values are strictly equal.
85    bool result = true;
86    napi_status status = napi_strict_equals(env, args[0], args[1], &result);
87    if (status != napi_ok) {
88        napi_throw_error(env, nullptr, "Node-API napi_get_cb_info fail");
89        return nullptr;
90    }
91    // Convert the result to napi_value and return napi_value.
92    napi_value returnValue = nullptr;
93    napi_get_boolean(env, result, &returnValue);
94    return returnValue;
95}
96```
97
98API declaration:
99
100```ts
101// index.d.ts
102export const strictEquals : (lhs: string, rhs: string | number) => boolean | void;
103```
104
105ArkTS code:
106
107```ts
108import hilog from '@ohos.hilog'
109import testNapi from 'libentry.so'
110try {
111  let lhs = "123";
112  let rhs = "123";
113  let str = "456";
114  let num = 123;
115  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, rhs));
116  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, str));
117  hilog.info(0x0000, 'testTag', 'Test Node-API napi_strict_equals: %{public}s', testNapi.strictEquals(lhs, num));
118} catch (error) {
119  hilog.error(0x0000, 'testTag', 'Test Node-API napi_strict_equals error: %{public}s', error.message);
120
121}
122```
123
124To print logs in the native CPP, add the following information to the **CMakeLists.txt** file and add the header file by using **#include "hilog/log.h"**.
125
126```text
127// CMakeLists.txt
128add_definitions( "-DLOG_DOMAIN=0xd0d0" )
129add_definitions( "-DLOG_TAG=\"testTag\"" )
130target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
131```
132