Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

how to include native module #15

Open
GaikwadPratik opened this issue Jul 18, 2019 · 5 comments
Open

how to include native module #15

GaikwadPratik opened this issue Jul 18, 2019 · 5 comments

Comments

@GaikwadPratik
Copy link

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.

  1. 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)
  1. 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)
@kylecarbs
Copy link
Member

How are you requiring the module? You shouldn't have to do writeModule since you are doing writeFiles to the node_modules dir.

@GaikwadPratik
Copy link
Author

@kylecarbs ,

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.?

@kylecarbs
Copy link
Member

writeFiles on node_modules should work. I believe that transformation might be causing some problems. Could you post your tsconfig.json?

cc: @code-asher

@GaikwadPratik
Copy link
Author

@kylecarbs ,

{
	"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"
	]
}

@GaikwadPratik
Copy link
Author

@kylecarbs ,

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants