how to include native module
GaikwadPratik opened this issue · 5 comments
I am trying to use nbin
for my project. I am following this example. However I am not able to include native module.
Here is my code:
import { mkdirpSync, writeFileSync } from "fs-extra";
import * as path from "path";
import { Binary } from "@coder/nbin";
const dirName = path.dirname(__dirname);
const bin = new Binary({
mainFile: path.join(dirName, "bin", "entry.js"),
suppressOutput: false,
target: 'linux'
});
bin.writeFiles(path.join(dirName, "lib", "**"));
bin.writeFiles(path.join(dirName, "bin", "**"));
bin.writeFiles(path.join(dirName, "..", "node_modules", "**"));
bin.writeModule(path.join(dirName, "..", "node_modules", "libvirt"));
bin.build().then((buffer) => {
const outDir = path.join(dirName, "out");
mkdirpSync(outDir);
writeFileSync(path.join(outDir, "test-exe"), buffer, {
mode: "755",
});
});
I am running above code on output generated by tsc
.
- If I don't use
bin.writeFiles(path.join(dirName, "..", "node_modules", "**"));
, then I get error:
internal/modules/cjs/loader.js:595
throw err;
^
Error: Cannot find module 'tslib'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:593:15)
at Function.Module._load (internal/modules/cjs/loader.js:519:25)
at Module.require (internal/modules/cjs/loader.js:649:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/home/pratik/hive/code/test/dist/bin/entry.js:3:17)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:717:10)
at Module.load (internal/modules/cjs/loader.js:611:32)
at tryModuleLoad (internal/modules/cjs/loader.js:550:12)
at Function.Module._load (internal/modules/cjs/loader.js:542:3)
- Irrespective of using
bin.writeModule(path.join(dirName, "..", "node_modules", "libvirt"));
,
I get the error:
/home/pratik/hive/code/test/node_modules/bindings/bindings.js:211
throw new Error(
^
Error: Could not find module root given file: "/home/pratik/hive/code/test/node_modules/libvirt/lib/index.js". Do you have a `package.json` file?
at Function.getRoot (/home/pratik/hive/code/test/node_modules/bindings/bindings.js:211:13)
at bindings (/home/pratik/hive/code/test/node_modules/bindings/bindings.js:82:32)
at Object.<anonymous> (/home/pratik/hive/code/test/node_modules/libvirt/lib/index.js:3:34)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:717:10)
at Module.load (internal/modules/cjs/loader.js:611:32)
at tryModuleLoad (internal/modules/cjs/loader.js:550:12)
at Function.Module._load (internal/modules/cjs/loader.js:542:3)
at Module.require (internal/modules/cjs/loader.js:649:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/home/pratik/hive/code/test/dist/lib/utilities/virt-utils.js:6:35)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:717:10)
at Module.load (internal/modules/cjs/loader.js:611:32)
at tryModuleLoad (internal/modules/cjs/loader.js:550:12)
at Function.Module._load (internal/modules/cjs/loader.js:542:3)
How are you requiring the module? You shouldn't have to do writeModule
since you are doing writeFiles
to the node_modules
dir.
I am assuming that you are asking about libvirt
module(right?).
from typescript
import * as virt from 'libvirt';
which then gets converted to JS as
const virt = tslib_1.__importStar(require("libvirt"));
Should I be doing writeFiles
on node_modules
.?
writeFiles
on node_modules
should work. I believe that transformation might be causing some problems. Could you post your tsconfig.json
?
cc: @code-asher
{
"compileOnSave": true,
"compilerOptions": {
"alwaysStrict": true,
"declarationMap": false,
"esModuleInterop": true,
"importHelpers": true,
"inlineSourceMap": true,
"inlineSources": true,
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"strict": true,
"strictNullChecks": true,
"target": "esnext"
},
"exclude": [
"node_modules",
"./typings",
"dist"
]
}
I just realized that I have forgotten to mention one thing. The error in the original question are post deployment and the paths in stack of the error are from the system on which I created executable. I hope this helps?