@@ -873,7 +873,7 @@ JavaScript Object associated with the `napi_ref`. Otherise, result
873873will be NULL.
874874
875875## Module registration
876- N-API modules are registered in the same manner as other modules
876+ N-API modules are registered in a manner similar to other modules
877877except that instead of using the `NODE_MODULE` macro the following
878878is used:
879879
@@ -885,32 +885,39 @@ The next difference is the signature for the `Init` method. For a N-API
885885module it is as follows:
886886
887887```C
888- void Init(napi_env env, napi_value exports, napi_value module, void* priv );
888+ napi_value Init(napi_env env, napi_value exports);
889889```
890890
891- As with any other module, functions are exported by either adding them to
892- the `exports` or `module` objects passed to the `Init` method.
891+ The return value from `Init` is treated as the `exports` object for the module.
892+ The `Init` method is passed an empty object via the `exports` parameter as a
893+ convenience. If `Init` returns NULL, the parameter passed as `exports` is
894+ exported by the module. N-API modules cannot modify the `module` object but can
895+ specify anything as the `exports` property of the module.
893896
894897For example, to add the method `hello` as a function so that it can be called
895898as a method provided by the addon:
896899
897900```C
898- void Init(napi_env env, napi_value exports, napi_value module, void* priv ) {
901+ napi_value Init(napi_env env, napi_value exports) {
899902 napi_status status;
900903 napi_property_descriptor desc =
901904 {"hello", Method, 0, 0, 0, napi_default, 0};
905+ if (status != napi_ok) return nullptr;
902906 status = napi_define_properties(env, exports, 1, &desc);
907+ if (status != napi_ok) return nullptr;
908+ return exports;
903909}
904910```
905911
906912For example, to set a function to be returned by the `require()` for the addon:
907913
908914```C
909- void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
915+ napi_value Init(napi_env env, napi_value exports) {
916+ napi_value method;
910917 napi_status status;
911- napi_property_descriptor desc =
912- {"exports", Method, 0, 0, 0, napi_default, 0} ;
913- status = napi_define_properties(env, module, 1, &desc) ;
918+ status = napi_create_function(env, "exports", Method, NULL, &method));
919+ if (status != napi_ok) return nullptr ;
920+ return method ;
914921}
915922```
916923
@@ -919,28 +926,30 @@ For example, to define a class so that new instances can be created
919926
920927```C
921928// NOTE: partial example, not all referenced code is included
922-
923- napi_status status;
924- napi_property_descriptor properties[] = {
929+ napi_value Init(napi_env env, napi_value exports) {
930+ napi_status status;
931+ napi_property_descriptor properties[] = {
925932 { "value", nullptr, GetValue, SetValue, 0, napi_default, 0 },
926933 DECLARE_NAPI_METHOD("plusOne", PlusOne),
927934 DECLARE_NAPI_METHOD("multiply", Multiply),
928- };
935+ };
929936
930- napi_value cons;
931- status =
932- napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons);
933- if (status != napi_ok) return;
937+ napi_value cons;
938+ status =
939+ napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons);
940+ if (status != napi_ok) return nullptr ;
934941
935- status = napi_create_reference(env, cons, 1, &constructor);
936- if (status != napi_ok) return;
942+ status = napi_create_reference(env, cons, 1, &constructor);
943+ if (status != napi_ok) return nullptr ;
937944
938- status = napi_set_named_property(env, exports, "MyObject", cons);
939- if (status != napi_ok) return;
945+ status = napi_set_named_property(env, exports, "MyObject", cons);
946+ if (status != napi_ok) return nullptr;
947+
948+ return exports;
949+ }
940950```
941951
942- For more details on setting properties on either the `exports` or `module`
943- objects, see the section on
952+ For more details on setting properties on objects, see the section on
944953[Working with JavaScript Properties][].
945954
946955For more details on building addon modules in general, refer to the existing API
0 commit comments