You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include <napi.h>
#include <string>
// https://github.com/nodejs/node-addon-examples/blob/main/1_hello_world/node-addon-api/hello.cc
// native C++ function that is assigned to "greetHello" property on "exports" object
Napi::String greetHello(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// call "helloUser" function from "greeting.cpp" file
// WARNING: We are passing hardcoded "MIKE" value for now
std::string result = helloUser( "MIKE" );
// return new "Nap::String" value
return Napi::String::New(env, result);
}
// callback method when module is registered with Node.js
Napi::Object Init(Napi::Env env, Napi::Object exports) {
// set a key on "exports" object
exports.Set(
Napi::String::New(env, "greetHello"), // property name => "greetHello"
Napi::Function::New(env, greetHello) // property value => "greetHello" function
);
// return "exports" object (always)
return exports;
}
// register "greet" module which calls "Init" method
NODE_API_MODULE(greet, Init)
I executed node-gyp configure :
raphy@raohy:~/native-greet-module$ node-gyp configure
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info find Python using Python version 3.10.6 found at "/usr/bin/python3"
gyp info spawn /usr/bin/python3
gyp info spawn args [
gyp info spawn args '/home/raphy/.nvm/versions/node/v18.12.1/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/home/raphy/native-greet-module/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/raphy/.nvm/versions/node/v18.12.1/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/raphy/.cache/node-gyp/18.12.1/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/home/raphy/.cache/node-gyp/18.12.1',
gyp info spawn args '-Dnode_gyp_dir=/home/raphy/.nvm/versions/node/v18.12.1/lib/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/home/raphy/.cache/node-gyp/18.12.1/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/home/raphy/native-greet-module',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.'
gyp info spawn args ]
gyp info ok
raphy@raohy:~/native-greet-module$
And then node-gyp build :
raphy@raohy:~/native-greet-module$ node-gyp build
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | linux | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/home/raphy/native-greet-module/build'
CXX(target) Release/obj.target/greet/src/greeting.o
CXX(target) Release/obj.target/greet/src/index.o
SOLINK_MODULE(target) Release/obj.target/greet.node
COPY Release/greet.node
make: Leaving directory '/home/raphy/native-greet-module/build'
gyp info ok
raphy@raohy:~/native-greet-module$
In ./src/ folder` :
index.tsx :
import React from "react";
import ReactDOM from "react-dom";
const greetModule = require('../build/Release/greet.node')
const App = () => (
<h1>My React and TypeScript App!</h1>
);
ReactDOM.render(
<App />,
document.getElementById("root")
);
Starting the react app I get no errors:
raphy@raohy:~/native-greet-module$ yarn start
yarn run v1.22.19
$ webpack serve --open
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:4000/
<i> [webpack-dev-server] On Your Network (IPv4): http://192.168.1.7:4000/
<i> [webpack-dev-server] On Your Network (IPv6): http://[fe80::f2e3:be71:cd02:bb1d]:4000/
<i> [webpack-dev-server] Content not from webpack is served from '/home/raphy/native-greet-module/build' directory
<i> [webpack-dev-middleware] wait until bundle finished: /
asset bundle.js 1.16 MiB [emitted] (name: main)
asset 5951ea98b41e9eadb7f0d975f38f4ae6.node 60.8 KiB [emitted] (auxiliary name: main)
runtime modules 23.7 KiB 11 modules
built modules 1.09 MiB [built]
modules by path ./node_modules/ 1.09 MiB
modules by path ./node_modules/webpack/hot/*.js 4.59 KiB 4 modules
modules by path ./node_modules/react/ 85.7 KiB 2 modules
modules by path ./node_modules/react-dom/ 1000 KiB 2 modules
modules by path ./node_modules/scheduler/ 17.3 KiB
./node_modules/scheduler/index.js 198 bytes [built] [code generated]
./node_modules/scheduler/cjs/scheduler.development.js 17.1 KiB [built] [code generated]
./src/index.tsx 331 bytes [built] [code generated]
./build/Release/greet.node 199 bytes [built] [code generated]
external "events" 42 bytes [built] [code generated]
external "path" 42 bytes [optional] [built] [code generated]
webpack 5.75.0 compiled successfully in 1766 ms
But I get this output, instead of the expected header message My React and TypeScript App! :
Following the indications found here: https://medium.com/jspoint/a-simple-guide-to-load-c-c-code-into-node-js-javascript-applications-3fcccf54fd32 and here https://github.com/nodejs/node-addon-examples/tree/main/1_hello_world/node-addon-api
I'm trying to understand how to effectively use node-addon-api for later include within electron C++ module.
In a simple react-typescript-webpack app I've put this:
package.json
:webpack.config.js
:const webpack = require("webpack")
const webpackDevServer = require("webpack-dev-server")
const path = require("path")
const Configuration = require("webpack")
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin")
tsconfig.json
:binding.gyp
:In
./src/
folder:greeting.h
:greeting.cpp
:#include
#include
#include "greeting.h"
Compiling and testing the
.cpp
file:index.cpp
:I executed
node-gyp configure
:And then
node-gyp build
:In
./src/
folder` :index.tsx
:Starting the
react app
I get no errors:But I get this output, instead of the expected header message
My React and TypeScript App!
:You can find the code in this
Repo
: https://github.com/raphael10-collab/native-greet-moduleWhat's wrong with my settings? And how to make this simple React-Typescript-Webpack with
node-addon
showing thereact page
?The text was updated successfully, but these errors were encountered: