Run CImg library with node-gyp error - node.js

I want to use CImg library to deal images in node.js,so i write an node addon to do it.
The compile is success, i run node-gyp build commond, that's ok.
But when i run the node program,the follow error occurs:
[root#localhost hcaptha]# node index.js
module.js:485
process.dlopen(filename, module.exports);
^
Error: /usr/local/nodejs/hcaptha/build/Release/hcaptha.node: undefined symbol: XSendEvent
at Object.Module._extensions..node (module.js:485:11)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/usr/local/nodejs/hcaptha/lib/hcap.js:1:75)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
the binding.gyp file is:
{
"targets":[
{
"target_name": "hcaptha",
"sources": [ "addon/hcaptha.cc" ,"addon/cap.cc"],
'cflags': ['-fexceptions','-O2','-Dcimg_use_png'],//the configure using CImg lib
'cflags_cc': ['-fexceptions','-O2','-Dcimg_use_png']
}
]
}
cap.cc code:
#include <node.h>
#include <string>
#include <iostream>
#include "cap.h"
#include "CImg-1.5.3/CImg.h"
using namespace v8;
Handle<Value> cap::create(const Arguments& args) {//create an image
HandleScope scope;
using namespace cimg_library;
CImg<unsigned char> captcha(256,64,1,3,0);//delete this line run ok!
return scope.Close(Boolean::New(1));
}
cap::cap(){};
cap::~cap(){};
index.js code:
var obj = require('../build/Release/hcaptha.node');
anyone can help me?

I finally find the result.
add line "libraries":['-lX11'] into the binding.gyp file,that's ok!
new binding.gyp file like this:
{
"targets":[
{
"target_name": "hcaptha",
"sources": [ "addon/hcaptha.cc" ,"addon/cap.cc"],
"cflags": ['-fexceptions','-O2','-Dcimg_use_png'],
"cflags_cc": ['-fexceptions','-O2','-Dcimg_use_png'],
"libraries":['-lX11']
}
]
}

Related

Imports from tsconfig paths not working with bazel: Cannot find module '#...'

Repo for reproduction: https://github.com/timothyaltemus/bazel-typescript-stackoverflow-example.
I am running into an issue where I am unable to use my tsconfig paths configuration for importing in my TypeScript files.
tsconfig.json:
{
...
"compilerOptions": {
"baseUrl": ".",
"paths" {
"#*": [
".",
"bazel-out/darwin-fastbuild/bin/*",
"bazel-out/k8-fastbuild/bin/*",
"bazel-out/x64_windows-fastbuild/bin/*",
"bazel-out/darwin-dbg/bin/*",
"bazel-out/k8-dbg/bin/*",
"bazel-out/x64_windows-dbg/bin/*"
]
},
"rootDirs": [
".",
"bazel-out/darwin-fastbuild/bin/",
"bazel-out/k8-fastbuild/bin/",
"bazel-out/x64_windows-fastbuild/bin/",
"bazel-out/darwin-dbg/bin/",
"bazel-out/k8-dbg/bin/",
"bazel-out/x64_windows-dbg/bin/"
]
...
}
...
}
Folder Structure:
apps
server
services
BUILD
hello_service.ts
BUILD
index.ts
libs
a_lib
BUILD
do_a.ts
b_lib
BUILD
do_b.ts
c_lib
BUILD
do_c.ts
Build
tsconfig.json
index.ts:
...
import { doToC } from '#libs/c_lib/do_c';
import {
HelloService,
HelloServiceImpl,
} from '#apps/server/services/hello_service';
...
Full Stacktrace:
INFO: Analyzed target //apps/server:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //apps/server:server up-to-date:
bazel-bin/apps/server/server.sh
bazel-bin/apps/server/server_loader.js
bazel-bin/apps/server/server_require_patch.js
INFO: Elapsed time: 1.994s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Error: Cannot find module '#libs/c_lib/do_c'
Require stack:
- /home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/server/server.sh.runfiles/typescript/apps/server/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (home/tim/.cache/bazel/_bazel_tim/4d04e993ef8ae996d277978eb4eab582/execroot/typescript/bazel-out/k8-fastbuild/bin/apps/apps/server/index.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Please see the example repo for an example of how to reproduce this error.

Typescript Type Annotations throws 'Unexpected token ":"'

I am trying to annotate my variables with types but when running the code it simply gives off an error:
let foo: number = 23;
console.log( foo );
// let foo: number = 23;
^
// SyntaxError: Unexpected token ':'
// [90m at Object.compileFunction (node:vm:352:18)[39m
// [90m at wrapSafe (node:internal/modules/cjs/loader:1031:15)[39m
// [90m at Module._compile (node:internal/modules/cjs/loader:1065:27)[39m
// [90m at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)[39m
// [90m at Module.load (node:internal/modules/cjs/loader:981:32)[39m
// [90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)[39m
// [90m at Function.executeUserEntryPoint [as runMain] // (node:internal/modules/run_main:79:12)[39m
// [90m at node:internal/main/run_main_module:17:47[39m
You have two options:
First compile it to plain JS with tsc myfile.ts and run the JS file with node myfile.js.
Or npm install ts-node which lets you run TS files like you would run JS files: ts-node myfile.ts

The "amdefine" can not load a custom module

I installed the requirejs package to optimize the work with paths to files. In general, I do the next steps:
I have an object with paths to file, that have next values:
"app/database": "/home/dmitry/projects/information-platform-api/app/utils/database"
After I initialize the requirejs mapping in app.js (main file):
requirejs.config({
baseUrl: __dirname,
nodeRequire: require,
paths: paths,
map: {
'*': paths
}
});
After, I need to initialize base models, and with help for I called all files from paths object. This looks like:
for (let file in paths) {
requirejs(paths[file]);
}
When I initializing the file, which has the dependency of the above "app/database", I have a message error: Error: Cannot find module 'app/database'.
Maybe, I don't understand "How works the amdefine", but I have opinion, what a requirejs and amdefine don`t link. Do you have any ideas, why it doesn't work?
A part of the file, which need to investigate:
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define([
'sequelize',
'app/database',
'model/student'
], (
Sequelize,
sequelize,
Student
) => { });
Result of my investigation:
The amdefine doesn't know about requirejs mapping.
Full error message:
Error: Tried loading "/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company" at /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company then tried node's require("/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company") and it failed with error: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2600:27
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39)
at Function.req.get (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2524:33)
at Object.localRequire [as require] (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1678:40)
at requirejs (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2047:24)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app.js:142:5)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/bin/www:7:13)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
originalError: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at req (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:144:31)
at stringRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:220:28)
at amdRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:113:24)
at /home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:177:24
at Array.map (<anonymous>)
at runFactory (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:176:25)
at define (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:277:13)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js:5:1)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2598:75
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js',
'/home/dmitry/projects/information-platform-api/app.js',
'/home/dmitry/projects/information-platform-api/bin/www'
]
},
moduleName: '/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company',
requireModules: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company'
]
}
Process finished with exit code 1
The solution is here guys. The amdefine have two params, first param it's a global variable module, and the second it's require function for calling the local files. By default, it was just require not requirejs which contains custom mapping.
if (typeof define !== 'function') {
var define = require('amdefine')(module, require('requirejs'));
}

SyntaxError when declaring a class in TypeScript

I'm getting an error when declaring a class:
// This is all on my test1.ts file content
class WDesign {
wModel: string;
wQuer: string;
}
let logWDesign = (wd : WDesign) => {
console.log(wd.wModel + " " + wd.wQuer);
}
let wd1 : WDesign;
wd1.wModel = "SDS-9985";
wd1.wQuer = "escarni";
logWDesign(wd1);
Then, to show on Node command prompt:
>node test1.ts
I get the following:
[filePath].test1.ts:3
wModel: string;
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Typescript ver 3.1.3
Node ver. 8.12.0
Visual Studio Code 1.28.2
You firstly have to transpile the .ts file .js by simply running the command
tsc
node test1.js
This will generate javascript corresponding to your typescript files. Node cannot directly understand typescript. You have to give it javascript code.
You can use typescript-node-starter code provided by Typescript here to save configuration hustle.
Post comments if further help is required.

Node.js - Error Unexpected Identifier

I'm using ubuntu 16.04 on vagrant to run this node.js.
node version is v6.2.2.
this is my code. i try to browsing for the solution using the keyword "SyntaxError: Unexpected identifier" but no luck.
function SignalFire(ID, startingLogs) {
this.fireID = ID;
this.logsLeft = startingLogs;
}
SignalFire.prototype = {
addLogs: function(numLogs) {
this.logsLeft += numLogs;
}
lightFire: function() {
console.log("Whooosh!");
}
}
this is the error i got:
/vagrant/inheritance.js:11
lightFire: function() {
^^^^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
Can you please help me with this?

Resources