Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript CommonJS replay clients are broken when using moduleResolution nodenext #400

Closed
thovden opened this issue Dec 6, 2022 · 0 comments · Fixed by #401
Closed

Comments

@thovden
Copy link
Contributor

thovden commented Dec 6, 2022

Expected Behavior

Typescript clients using "moduleResolution": "nodenext" with "module": "commonjs" should be able to import the package. I have a large monorepo with a mix of CommonJS and ESM modules, and want to use Node 16+ module resolution across the monorepo.

Actual Behavior

Typescript is unable to import the file, with error:

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("@puppeteer/replay")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to 'puppeteer-client/package.json'.ts

The issue is discussed here: microsoft/TypeScript#50466

Steps to Reproduce the Problem

  1. Clone https://github.com/signatu/puppeteer-replay-import-error
  2. npm i
  3. npm run build

The repository is very simple, and the main trigger is the tsconfig.json below:

{
        "module": "commonjs",
        "moduleResolution": "nodenext"
}

Fix

The fix is to provide a separate type file cjs/main.d.cts for the .cjs CommonJS module, and refer to that in package.json:

  "exports": {
    ".": {
      "import": {      
        "types": "./lib/main.d.ts",
        "default": "./lib/main.js"
      }, 
      "require": {
        "types": "./lib/cjs/main.d.cts",
        "default": "./lib/cjs/main.cjs"
      }
    }
  },

I'll submit a PR for this change.

Specifications

  • Version: 2.6.0
  • Platform: MacOS 13.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant