Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Invalid type definitions - static import of ESM types from CJS types #447

Open
benmccann opened this issue Dec 1, 2023 · 6 comments
Open
Labels
type: bug code to address defects in shipped code

Comments

@benmccann
Copy link

benmccann commented Dec 1, 2023

Describe the bug

../../node_modules/.pnpm/@[email protected]/node_modules/@netlify/functions/dist/function/v2.d.ts:1:30 - error TS1479: 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("@netlify/serverless-functions-api")' call instead.

1 export type { Context } from '@netlify/serverless-functions-api';
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in ../../node_modules/.pnpm/@[email protected]/node_modules/@netlify/functions/dist/function/v2.d.ts:1

Configuration

Import the library in a project which has the following in the tsconfig.json:

		"module": "node16",
		"moduleResolution": "node16",

This issue can be worked around with "skipLibCheck": true, so ensure that is not set when reproducing. It is not set by default.

@benmccann benmccann added the type: bug code to address defects in shipped code label Dec 1, 2023
@Skn0tt
Copy link
Contributor

Skn0tt commented Dec 5, 2023

Thanks for reporting this! It's good to know there's a workaround, i'll add it to our internal triage.

@Skn0tt
Copy link
Contributor

Skn0tt commented Dec 5, 2023

I'm a bit surprised about TypeScript emitting this error for a .d.ts file. That can't use require anyways, right?

@benmccann
Copy link
Author

They're not using require, but you can only import CJS from ESM if you use a dynamic import. Another possible solution besides updating the types would be to distribute this package as ESM.

@Skn0tt
Copy link
Contributor

Skn0tt commented Dec 5, 2023

They're not using require, but you can only import CJS from ESM if you use a dynamic import.

We're only importing @netlify/serverless-functions-api for types:

https://github.com/netlify/functions/blob/main/src/function/v2.ts#L1

So I don't think that an actual import line shows up in the generated JS file. This seems to be purely about the declaration file that TypeScript generates, and I would have expected TypeScript to generate it in a way that's compliant with CJS, because that's how it's configured:

"module": "commonjs" /* Specify what module code is generated. */,

Anyways, distributing this as ESM makes sense to me. @eduardoboucas do you see any reason why this might become a problem? Maybe consumption from within Next.js sites?

@ascorbic
Copy link
Contributor

I think this will be fixed by #473

@eltigerchino
Copy link

eltigerchino commented Jun 9, 2025

I can verify that #473 fixes this. We're on version 3.0.0 and no longer receive this error so we've removed the skipLibCheck option https://github.com/sveltejs/kit/pull/13865/files#diff-2e13544b78a08098dd2ff51de4edd9d401524457c2ee4cdfe0b71cab55d25676

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

4 participants