Skip to content

The module can't be synchronously required in CJS modules when using modern moduleResolutions #1791

@Andarist

Description

@Andarist

Describe the bug

93d44d4 fixed the types visibility issue reported by me here: #1787 but it didn't quite fix in full how this package can be required at type level

The input code:

// @moduleResolution: node16
// @module: commonjs
import {} from 'solid-js' // types: 1.7.7

results in

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("solid-js")' call instead.
  To convert this file to an ECMAScript module, create a local package.json file with `{ "type": "module" }`.(1479)

You can also verify this here: https://arethetypeswrong.github.io/?p=solid-js%401.7.7

Your Example Website or App

https://www.typescriptlang.org/play?moduleResolution=3&target=99&module=1&ts=5.1.3#code/PTAEAEFsHsBMFcA2BTASsgztR8AuBLaAOwC5Qi5kBGANgCgQIYEUyBjaSGIgKwzvyQADtABOuUAG8AvqABmozqADkWRPlgBaPstCNcATyGYyVAHQB2S0A

Steps to Reproduce the Bug or Issue

Configure the tsconfig.json using the mentioned options

Expected behavior

Since at runtime I can require('solid-js'), I should be also able to load this synchronously at type-level

Screenshots or Videos

No response

Platform

N/A

Additional context

TS, rightfully, assumes that this module can't be required in CJS and it might suggest using a dynamic import. Since your package.json#type is set to module and you are using .d.ts files for your type declarations, TS can only assume that those correspond to .js files and that they represent non-requireable modules.

The only true solution to this problem is to ship your types twice. With your current setup, you should be shipping .d.ts and .d.cts declarations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs WelcomeAn issue which might not be prioritized right away but would be open to community contributionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions