Skip to content

Native node modules from extensions #658

Open

Description

I’m looking to build an extension but one of the packages I need to depend on is a native module. When I do an npm install it installs the package just fine, I can launch a node shell and interact with it, etc. But when I go to use the package from within my plugin I get an error as it’s trying to load the ia32 build of the native module not the x64 which was compiled by node-gyp.

A quick bit of debugging indicates that the problem stems by process.arch returning ia32 when running in VS Code when my machine is a x64 machine (Win10 x64).

So is there some way to either:

  • Have VS Code run an x64 process
  • Have VS Code’s ia32 process do my npm restore (and subsequent compile with node-gyp)

Otherwise I fear that my extension might be dead in the water 😦

Activity

  1. added
    feature-request
    on Nov 26, 2015
  2. aaronpowell commented on Nov 26, 2015

    @aaronpowell
    ContributorAuthor

    I'm doing some more investigation on this to see if I can unblock myself by getting an ia32 version of the module loaded, so I installed the x86 version of node.js, installed the native module (which in turned got the right binary).

    Now I get the following error:

    native-module-error

    Are native modules not supported in extensions?

  3. joaomoreno commented on Nov 26, 2015

    @joaomoreno
    Contributor

    The good: Nothing really.
    The bad: Code extensions unfortunately don't support native extensions.
    The ugly: There is a workaround. Bear with me:

    • Your native packages must be compiled against the exact same version of node and v8 that Code is. Specifically:
      • npm_config_disturl=https://atom.io/download/atom-shell
      • npm_config_target= whatever value is there on electronVersion
      • npm_config_arch= whatever the bit architecture you need
      • HOME= something other that ~, like ~/.electron-gyp, so node-gyp and npm don't confuse themselves.
    • The extension package must contain the binaries for all platforms Code supports: win32-ia32, darwin, linux-ia32 and linux-x64. It would then dynamically require the correct binary for the running platform at runtime. bindings might help you there.
  4. aaronpowell commented on Nov 26, 2015

    @aaronpowell
    ContributorAuthor

    Yeah I just came across this walkthrough which somewhat outlines the "ugly" side of things 😛.

    Native modules are a pain, but I might keep at it and see where I can get to (or see if the native module can be reimplemented in "plain ol' JavaScript", but somehow I don't expect it to be possible.

  5. joaomoreno commented on Nov 26, 2015

    @joaomoreno
    Contributor

    We use the npm way in our build scripts, btw.

  6. aaronpowell commented on Nov 30, 2015

    @aaronpowell
    ContributorAuthor

    I've been playing around with the easy way and got this far:

    • electron-rebuild generated a binary output
    • My extension still failed as the path was not matching the generated output
    • Modified the folder of the generated output and it worked

    So - I now have a "working" native node package running in a VSCode extension. But I was curious as to why it wanted a folder of node-v46... to load the binary. It seems that the logic to load the binary uses the node-gyp build/versioning lookup which might be somewhat broken in VSCode. The failure is that internally it does some checks to whether it's node, node-wekbit or electron, but process.versions.electron is undefined in the VSCode plugin. My guess is that because the extension is running out-of-process it's not getting that passed through, which in turn makes it a little unhappy.

    Anyway I can at least get the extension launched, so chalk that up to success number 1 😁.

  7. aaronpowell commented on Dec 1, 2015

    @aaronpowell
    ContributorAuthor

    I've hit another problem that I'm not sure if it's related to it being a native module or if it's another bug, the native module is being reported as not found by VSCode's editor and the TypeScript compiler (when running the package step).

    If I run from the debugger it works just fine, the native module is loaded, executed, etc, so I know it's there, but tsc doesn't seem to locate it. I think it's a TypeScript issue than a VSCode issue, but I can't find any info on it. Is it known at the VSCode end?

  8. joaomoreno commented on Dec 1, 2015

    @joaomoreno
    Contributor

    It seems that you just need TypeScript typings to go along your native module. Try placing a file in your extension similar to this:

    mylib.d.ts

    declare module 'mylib' {
        export function foo(arg: number): void;
    }

    Then, both VSCode and TypeScript would drop the errors when you'd import the module:

    import * as mylib from 'mylib';
  9. aaronpowell commented on Dec 2, 2015

    @aaronpowell
    ContributorAuthor

    ah, I didn't realise you need to have everything with a .d.ts. All sorted and now it's on the marketplace - https://marketplace.visualstudio.com/items/aaronpowell.vscode-espruino (although I'm not sure it'll be installable for anyone else yet).

    Is there any way (easy or ugly) to work out the version of Electron when installing a plugin?

  10. joaomoreno commented on Dec 2, 2015

    @joaomoreno
    Contributor

    You must always compile it against the version of Electron that VSCode depends on, considering the VSCode version you're targeting. So, for VSCode 0.10.3 it is Electron 0.34.1.

  11. aaronpowell commented on Dec 2, 2015

    @aaronpowell
    ContributorAuthor

    Yeah I found that out, I'm just trying to avoid shipping a precompiled version of serialport and instead compile it as an install step, but I need to work out what version of VSCode is running (and that tells me what version of Electron)

  12. joaomoreno commented on Dec 3, 2015

    @joaomoreno
    Contributor

    Very tricky and it's exactly the reason why we haven't tackled this yet: every user that would install your extension would need the build tooling installed in their system (VS, gcc, etc.).

  13. aaronpowell commented on Dec 3, 2015

    @aaronpowell
    ContributorAuthor

    Yeah that's a fair point, I guess shipping the compiled module in my extension is probably a good idea (or at least the binary) and get it done for each platform.

    I might be able to make an assumption of the build tools (given that it's work working with microprocessors) but it's solid assumption.

  14. 28 remaining items

  15. pythoulon commented on May 12, 2021

    @pythoulon

    Brent Mealhouse (@bmealhouse) Finally got a chance to take a look at prebuildify. The problem I have is that my extension module is not native per se, but it depends on a couple of native modules. I'm not sure how to use prebuildify in that context. Ideally I would like to deliver my module with the prebuilt versions of those modules (which, as far as I understand, do not come with prebuilt binaries).
    Anybody has experience on the matter ? Thanks.

  16. bmealhouse commented on May 15, 2021

    @bmealhouse

    pythoulon - You could fork those native modules and prebuildify them for VS code.

  17. pythoulon commented on May 17, 2021

    @pythoulon

    Brent Mealhouse (@bmealhouse) Thanks for the hint. Hadn't thought of it... Will try that.

  18. samuela commented on Oct 17, 2021

    @samuela

    I'm running into this issue currently. In particular, I'm trying to use nodegit which has a native dependency on libgit2. At first I was seeing some NODE_MODULE_VERSION mismatch errors. Those were resolved by downgrading my node version to 15.x.

    But now I'm seeing an error:

    Module did not self-register: '/Users/skainswo/dev/cuddlefish/vscode-extension/node_modules/nodegit/build/Release/nodegit.node'..
    

    Is there a plan for a more robust native dependency solution for VSCode extensions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extensionsfeature-request

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions