Skip to content

TS has no way to import native modules with ESM syntax #61486

@CamJN

Description

@CamJN

🔎 Search Terms

  • --experimental-addon-modules
  • ESM
  • native extension
  • .node

🕗 Version & Regression Information

  • This has always been the case
  • This is the behaviour in every version I tried, and I checked the FAQ for entries about importing native modules but none exist or I missed them

⏯ Playground Link

I don't see any way to include a native extension in the playground, so that's not possible. Here's a repo: https://github.com/getargv/getargv.js/tree/feature/esm

💻 Code

import addon from '../build/Release/getargv_native.node';

🙁 Actual behavior

$ npm run build

> @camjn/getargv@0.0.31 prebuild
> tsc

lib/binding.ts:3:19 - error TS2307: Cannot find module '../build/Release/getargv_native.node' or its corresponding type declarations.

3 import addon from '../build/Release/getargv_native.node';
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

🙂 Expected behavior

Since node offers the --experimental-addon-modules flag and every node version from node20 on supports esm syntax, I expect to be able to use esm syntax to import native modules in typescript too.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions