Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

TS and node16 modules #891

Closed
Closed

Description

@danielepolencic

Codemirror does not compile with typescript 4.7 when "module": "node16" and "moduleResolution": "node16" in the tsconfig. Error:

some-path/src/code-mirror.tsx(1,40): error TS7016: Could not find a declaration file for module 'codemirror'. '/private/var/tmp/_bazel/198d81e4f6aafdc263b492f2c04a5df7/sandbox/darwin-sandbox/959/execroot/project/node_modules/codemirror/dist/index.cjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/codemirror` if it exists or add a new declaration (.d.ts) file containing `declare module 'codemirror';`

Why

In a few words, TS introduced new ways to publish and consume type definitions and TS files depending on if you code using ESM or Commonjs.

What I tried to fix it

According to this, adding an index.d.cts does the trick. So I copy-pasted the index.d.ts and renamed it. Then I removed types from the package.json and it did not work.

Also

The same issue seems to affect other packages as well:

  • @codemirror/view
  • @codemirror/state
  • @codemirror/lang-markdown

Context

  • Node 16.2.0
  • codemirror 6.0.1
  • @codemirror/lang-markdown 6.0.0
  • Typescript 4.7.2

Activity

  1. marijnh commented on Jul 5, 2022

    @marijnh
    Member

    I expect this will break with a lot of npm packages. Weird decision of the TypeScript team to define that option in such a way that it doesn't work with the way most packages are being published (single declaration file using ES exports). Does anybody know about a non-invasive way around this (that doesn't break with the old resolution setting)?

  2. revelt commented on Sep 6, 2022

    @revelt

    I had similar troubles with types not recognised on "strict" mode on TS 4.7, similar to here, and in the end we found that types should also go inside exports, as the first entry. Adapting to @codemirror/view, the relevant keys would be:

    // package.json
    {
      "name": "@codemirror/view",
      "type": "module",
      "exports": {
        "types": "./dist/index.d.ts", // <-- add this, notice the dot-slash, also the position is important
        "import": "./dist/index.js",
        "require": "./dist/index.cjs"
      },
      "types": "dist/index.d.ts" // <-- still keep, notice no dot in front
    }

    I updated all my open source npm packages that way.

  3. jodyheavener commented on Oct 31, 2022

    @jodyheavener

    Here's a bit more context as well: microsoft/TypeScript#49160 (comment)

    I'm eager to see this fixed, so I would be happy to take a stab at it.

  4. prichey commented on Jan 24, 2023

    @prichey

    I have an open PR for this in codemirror/state: codemirror/state#3. If it gets traction I'm happy to introduce similar requests across all the other repos as well. I believe this approach is sufficiently non-invasive (as it is backwards / forwards compatible with both Node and TS)

  5. marijnh commented on Aug 30, 2023

    @marijnh
    Member

    I think this should be fixed since June now.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions