Building NodeJs module with boost (or any other library for that matter) - c++

I'm trying to build a nodejs module with C++ and Ubuntu 13.04 using some boost headers as shown below:
#include <iostream>
#include <string>
#include <node/node.h>
#include <v8.h>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
std::string foobar = "8";
return scope.Close(String::New("world"));
}
void init(Handle<Object> exports){
exports->Set(String::NewSymbol("Hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
However, when compiling with node-gyp, I get the following errors:
sam#ubuntu:~/workspace_cpp/NodeTest/src$ node-gyp build gyp info it
worked if it ends with ok gyp info using node-gyp#0.10.9 gyp info
using node#0.10.15 | linux | x64 gyp info spawn make gyp info spawn
args [ 'BUILDTYPE=Release', '-C', 'build' ] make: Entering directory
/home/sam/workspace_cpp/NodeTest/src/build' CXX(target)
Release/obj.target/hello/NodeTest.o In file included from
/usr/include/boost/numeric/conversion/converter.hpp:14:0,
from /usr/include/boost/numeric/conversion/cast.hpp:33,
from /usr/include/boost/lexical_cast.hpp:66,
from ../NodeTest.cpp:13: /usr/include/boost/numeric/conversion/converter_policies.hpp: In
member function ‘void
boost::numeric::def_overflow_handler::operator()(boost::numeric::range_check_result)’:
/usr/include/boost/numeric/conversion/converter_policies.hpp:162:31:
error: exception handling disabled, use -fexceptions to enable make:
*** [Release/obj.target/hello/NodeTest.o] Error 1 make: Leaving directory/home/sam/workspace_cpp/NodeTest/src/build' gyp ERR! build
error gyp ERR! stack Error: make failed with exit code: 2 gyp ERR!
stack at ChildProcess.onExit
(/usr/local/lib/node_modules/node-gyp/lib/build.js:267:23) gyp ERR!
stack at ChildProcess.EventEmitter.emit (events.js:98:17) gyp ERR!
stack at Process.ChildProcess._handle.onexit
(child_process.js:789:12) gyp ERR! System Linux 3.8.0-19-generic gyp
ERR! command "node" "/usr/local/bin/node-gyp" "build" gyp ERR! cwd
/home/sam/workspace_cpp/NodeTest/src gyp ERR! node -v v0.10.15 gyp
ERR! node-gyp -v v0.10.9 gyp ERR! not ok
I cannot find anything on the web for how to get node-gyp to build with other libraries such as boost. Does anybody have any insight or experience with this? My ultimate goal is to make a SOAP module using gsoap.
EDIT
I'm assuming that I have to edit my binding.gyp file in some way to allow for boost to be compiled. As it sits, this file currently looks like this:
{ "targets": [
{
"target_name": "hello",
"sources": [ "NodeTest.cpp" ]
} ] }

For anybody else that has this error, the key is enabling exceptions for node-gyp. In your bindings.gyp file, make sure you include this
...
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ]
...
I found my solution in this forum post: GitHub

Related

Unable to install node-rdkafka package

I am unable to install node-rdkafka in any of my projects recently by using
npm i node-rdkafka
or if I had a package-lock.json using
npm ci
this is the error I am getting with node 14 and npm 6.14.17 -
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion#1, but package-lock.json was generated for lockfileVersion#2. I'll try to do my best with it!
> #newrelic/native-metrics#7.1.1 install /Users/subhrangshu/Documents/MyProject/node_modules/#newrelic/native-metrics
> node ./lib/pre-build.js install native_metrics
============================================================================
Attempting install in native-metrics module. Please note that this is an
OPTIONAL dependency, and any resultant errors in this process will not
affect the general performance of the New Relic agent, but event loop and
garbage collection metrics will not be collected.
============================================================================
> /Users/subhrangshu/.nvm/versions/node/v14.21.2/bin/node /Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js clean configure
> /Users/subhrangshu/.nvm/versions/node/v14.21.2/bin/node /Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build -j 4 native_metrics
CXX(target) Release/obj.target/native_metrics/src/GCBinder.o
CXX(target) Release/obj.target/native_metrics/src/native_metrics.o
CXX(target) Release/obj.target/native_metrics/src/LoopChecker.o
CXX(target) Release/obj.target/native_metrics/src/RUsageMeter.o
In file included from ../src/GCBinder.cpp:7:
../src/GCBinder.hpp:8:10: fatal error: map: No such file or directory
8 | #include <map>
| ^~~~~
../src/LoopChecker.cpp:7:10: fatal error: algorithm: No such file or directory
7 | #include <algorithm>
| ^~~~~~~~~~~
../src/RUsageMeter.cpp:7:10: fatal error: cstring: No such file or directory
7 | #include <cstring>
| ^~~~~~~~~
compilation terminated.
compilation terminated.
compilation terminated.
make: *** [Release/obj.target/native_metrics/src/GCBinder.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [Release/obj.target/native_metrics/src/LoopChecker.o] Error 1
make: *** [Release/obj.target/native_metrics/src/RUsageMeter.o] Error 1
In file included from /Users/subhrangshu/Library/Caches/node-gyp/14.21.2/include/node/node.h:67,
from ../../../nan/nan.h:58,
from ../src/native_metrics.cpp:7:
/Users/subhrangshu/Library/Caches/node-gyp/14.21.2/include/node/v8.h:22:10: fatal error: memory: No such file or directory
22 | #include <memory>
| ^~~~~~~~
compilation terminated.
make: *** [Release/obj.target/native_metrics/src/native_metrics.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack at ChildProcess.emit (events.js:400:28)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:285:12)
gyp ERR! System Darwin 22.2.0
gyp ERR! command "/Users/subhrangshu/.nvm/versions/node/v14.21.2/bin/node" "/Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "-j" "4" "native_metrics"
gyp ERR! cwd /Users/subhrangshu/Documents/MyProject/node_modules/#newrelic/native-metrics
gyp ERR! node -v v14.21.2
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
Failed to execute native-metrics install: No pre-built artifacts for your OS/architecture.
> node-rdkafka#2.14.0 install /Users/subhrangshu/Documents/MyProject/node_modules/node-rdkafka
> node-gyp rebuild
CXX(target) Release/obj.target/node-librdkafka/src/binding.o
../src/binding.cc:10:10: fatal error: iostream: No such file or directory
10 | #include <iostream>
| ^~~~~~~~~~
compilation terminated.
make: *** [Release/obj.target/node-librdkafka/src/binding.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack at ChildProcess.emit (events.js:400:28)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:285:12)
gyp ERR! System Darwin 22.2.0
gyp ERR! command "/Users/subhrangshu/.nvm/versions/node/v14.21.2/bin/node" "/Users/subhrangshu/.nvm/versions/node/v14.21.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/subhrangshu/Documents/MyProject/node_modules/node-rdkafka
gyp ERR! node -v v14.21.2
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: #newrelic/native-metrics#7.1.1 (node_modules/#newrelic/native-metrics):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: #newrelic/native-metrics#7.1.1 install: `node ./lib/pre-build.js install native_metrics`
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Exit status 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-rdkafka#2.14.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-rdkafka#2.14.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/subhrangshu/.npm/_logs/2023-01-13T04_21_16_920Z-debug.log
based on this question -
GYP ERR! build error. stack Error: 'make' failed with exit code 2
and other similar articles -
Have tried upgrading to macOS 13.1
Tried installing using node 16, node 15, node 18 and node 14
Removed the package-lock.json and node_modules folder and reinstalled using npm i instead.
Removed and reinstalled xcode-select --install
Even though the error shown is - it is unable to read c++ headers, have tried compiling a Hello World C++ program using g++ main.cpp -o main or make main and both compiled the program successfully
Have updated and upgraded brew
Current versions -

Errors from `node-gyp build`: invalid value 'gnu++1y' in '-std=gnu++1y'

UPDATE. Output of gcc -v as requested:
→ gcc -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
I am trying to follow the Node.js official addons example
The node-gyp configure step runs without errors.
However, the following node-gyp build step is failing:
→ node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp#5.0.2
gyp info using node#10.16.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/addon/hello.o
error: invalid value 'gnu++1y' in '-std=gnu++1y'
make: *** [Release/obj.target/addon/hello.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/Users/dmitrizaitsev/.npm-global/lib/node_modules/node-gyp/lib/build.js:190:23)
gyp ERR! stack at ChildProcess.emit (events.js:198:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Darwin 12.6.0
gyp ERR! command "/usr/local/bin/node" "/Users/dmitrizaitsev/.npm-global/bin/node-gyp" "build"
gyp ERR! cwd /Users/dmitrizaitsev/test/addons/official
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v5.0.2
gyp ERR! not ok
The first error message is
invalid value 'gnu++1y' in '-std=gnu++1y'
What does it mean and how can this error possibly be fixed?

Node-gyp build fail. Can't locate library headerfile in sub-sub-directory of included directory

I want to use the DronecodeSDK (C++) library within a Node.js application. I'm currently unable to build the node-addon that includes the DronecodeSDK headers. It's only the headers within the sub-subdirectories that node-gyp can't find.
#include <dronecode_sdk/dronecode_sdk.h> is included just fine, but #include <dronecode_sdk/plugins/action/action.h> isn't.
If I move the action.h file to dronecode_sdk/action.h then the include works just fine. It's only when it's moved inside the subdirectory of plugins that it isn't found.
My binding.gyp file:
{
"targets": [
{
"include_dirs" : [
"<!(node -e \"require('nan')\")",
"../lib/DronecodeSDK/install/include"
],
"libraries": [ "../lib/DronecodeSDK/install/lib/libdronecode_sdk.a" ],
"target_name": "DronecoreSDK",
"sources": [ "DronecoreSDKBridge.cc" ]
}
]
}
Two include statements in 'DronecodeSDKBridge.cc' :
#include <dronecode_sdk/dronecode_sdk.h>
#include <dronecode_sdk/plugins/action/action.h>
I get the following error message:
gyp info it worked if it ends with ok
gyp info using node-gyp#3.8.0
gyp info using node#10.15.0 | darwin | x64
gyp info spawn /usr/local/bin/python2
gyp info spawn args [ '/usr/local/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 '/Users/aokholm/src/kitex/_3_GroundStation/electron-GC/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/usr/local/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/aokholm/.node-gyp/10.15.0/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/Users/aokholm/.node-gyp/10.15.0',
gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/Users/aokholm/.node-gyp/10.15.0/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/Users/aokholm/src/kitex/_3_GroundStation/electron-GC',
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 make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/DronecoreSDK/DronecoreSDKBridge.o
../DronecoreSDKBridge.cc:10:10: fatal error: 'dronecode_sdk/plugins/action/action.h' file not found
#include <dronecode_sdk/plugins/action/action.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [Release/obj.target/DronecoreSDK/DronecoreSDKBridge.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/node-gyp/lib/build.js:262:23)
gyp ERR! stack at ChildProcess.emit (events.js:182:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Darwin 18.2.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/aokholm/src/kitex/_3_GroundStation/electron-GC
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Could it be that sub-subdirectories just doesn't work, or is the error somewhere else? I have checked that the file permissions of all the headers files are readable.
Node-gyp paths are quite mysterious.
The library path ../lib/DronecodeSDK/install/lib/libdronecode_sdk.a is correct. However the include path ../lib/DronecodeSDK/install/include was incorrect.
The reason why node-gyp could find the dronecode_sdk.h header anyway is that that it by default searches for headers in the two most top level directories of the project.
Changing the include path to lib/DronecodeSDK/install/include solved the problem.

node-gyp build error "calling a protected constructor of class 'v8::HandleScope'" on mac

I use node-gyp to build my c++ library for node, and copy some examples on the web.
callback.cc
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const v8::internal::Arguments& args){
HandleScope scope;
return scope.Close(String::New("hello,world"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
binding.gyp
{
"targets": [
{
"target_name": "hello",
"sources": [ "callback.cc" ]
}
]
}
run node-gyp configure ok.
run node-gyp build fail, error:
gyp info it worked if it ends with ok
gyp info using node-gyp#3.6.0
gyp info using node#7.9.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
CXX(target) Release/obj.target/hello/callback.o
../callback.cc:5:15: error: calling a protected constructor of class 'v8::HandleScope'
HandleScope scope;
^
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:908:13: note: declared protected here
V8_INLINE HandleScope() {}
^
../callback.cc:6:16: error: no member named 'Close' in 'v8::HandleScope'
return scope.Close(String::New("hello,world"));
~~~~~ ^
../callback.cc:6:30: error: no member named 'New' in 'v8::String'
return scope.Close(String::New("hello,world"));
~~~~~~~~^
../callback.cc:9:65: error: cannot initialize a parameter of type 'v8::Isolate *' with an lvalue of type 'Handle<v8::Value> (const v8::internal::Arguments &)'
(aka 'v8::Local<v8::Value> (const v8::internal::Arguments &)')
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
^~~~~~
/Users/zhouweiming/.node-gyp/7.9.0/include/node/v8.h:5079:16: note: passing argument to parameter 'isolate' here
Isolate* isolate, FunctionCallback callback = 0,
^
../callback.cc:9:24: error: no member named 'NewSymbol' in 'v8::String'
exports->Set(String::NewSymbol("hello"),FunctionTemplate::New(Method)->GetFunction());
~~~~~~~~^
5 errors generated.
make: *** [Release/obj.target/hello/callback.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/.node-gyp_npminstall/node_modules/.node- gyp#3.6.0/lib/build.js:285:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:194:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Darwin 16.5.0
gyp ERR! command "/usr/local/Cellar/node/6.9.1/bin/node" "/usr/local/bin/node-gyp" "build"
gyp ERR! cwd /Users/zhouweiming/data/project/electron-projects/dll
gyp ERR! node -v v7.9.0
gyp ERR! node-gyp -v v3.6.0
gyp ERR! not ok
I'm not a c++ coder, please help me.
node: 7.9.0
node-gyp: 3.6.0
xcode: 8.1
mac: 10.12.4
gcc:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I have solved it. Because the c++ code in demo is outdated.
the new codes like:
#include <node/v8.h>
#include <node/node.h>
using namespace node;
using namespace v8;
void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "Hello world"));
}
void init(v8::Local<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Method);
}
NODE_MODULE(binding, init);
there are too many outdated examples on the web, I will keep this question

node-xmpp build fails on windows 7

I'm trying to npm install -g node-xmpp and the build fails on the c++ compilation part with the following error:
LINK : fatal error LNK1181: cannot open input file 'kernel32.lib' [C:\Users\jhon\AppData\Roaming\npm\node_modules\node-xmpp\node_modules\node-expat\build\node_expat.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:267:23)
gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\shon\AppData\Roaming\npm\node_modules\node-xmpp\node_modules\node-expat
gyp ERR! node -v v0.10.24
gyp ERR! node-gyp -v v0.12.1
gyp ERR! not ok
I've tried removing and re-installing all my visual studio components, with no luck (currently using VS 2013 express).
if I look at the environment vars on visual studio command prompt, I can see that windowssdkdir points to C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\ and kernel32.lib is there at the Lib dir.
I'm lost, any help would be appreciated