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

Fix compatibility with Node16/NodeNext moduleResolution #1200

Closed
AsyncBanana opened this issue Dec 29, 2023 · 6 comments
Closed

Fix compatibility with Node16/NodeNext moduleResolution #1200

AsyncBanana opened this issue Dec 29, 2023 · 6 comments
Labels

Comments

@AsyncBanana
Copy link

When setting ModuleResolution to NodeNext in a project's tsconfig.json and using ESM within that project, superstruct's types fail to be discovered. I am fairly sure this is due to the fact that index.d.ts imports other type declarations without file extensions.

It would be a fairly simple fix to change imports to use .js file extensions, which do work, even for type declarations, and shouldn't break anything.

@mnahkies
Copy link

mnahkies commented Jan 9, 2024

Duplicates #1160

Would be nice to get a resolution to this / #1160 - just ran into this today 🤞

@ciscoheat
Copy link

Same here, cannot integrate Superstruct in Superforms v2 due to this.

@ciscoheat
Copy link

Opened a PR for this issue in #1211

@arturmuller arturmuller pinned this issue Mar 11, 2024
@arturmuller arturmuller changed the title Types incompatible with NodeNext moduleResolution Fix compatibility with Node16/NodeNext moduleResolution Mar 11, 2024
@nerochiaro
Copy link

I am testing version 1.0.5-0 which, as I understand it, is a pre-release for the fix to this issue.
It works OK if I put "moduleResolution":"node16" in my tsconfig.json.

However it seems to fail for me when I also use "module": "node16" along with it.
This is what is recommended for node 20 by https://www.npmjs.com/package/@tsconfig/node20 so I would expect superstruct to work with this common configuration. But maybe I am misunderstanding something.

My index.ts file is like this:

import { object, string } from 'superstruct';
const User = object({ foo: string() });

and I get this error:

index.ts:1:32 - 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("superstruct")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/home/ugo/superstructure-test/package.json'.

1 import { object, string } from 'superstruct';

Am I missing something or should this work too ?

@arturmuller
Copy link
Collaborator

@nerochiaro The TS error actually explains it correctly — you are importing an ECMAScript module (superstruct in this case) from a CommonJS module. That's currently not allowed by Node.js.

The underlying cause is a bit obscured by TypeScript in this case, but it's the equivalent of doing something like this:

// File: index.cjs <- a CommonJS module because of .cjs
const { hello } = require("./hello.mjs")
hello()
// File: hello.mjs <- a ECMAScript module because of .mjs
export function hello() {
  console.log("hello world")
}

Trying to run this with Node you get the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module <path>/hello.mjs not supported.
Instead change the require of <path>/hello.mjs to a dynamic import() which is available in all CommonJS modules.

In TypeScript codebases, I find it best to set the package.json type field to "module", which solves this issue.

{
  "type": "module",

  // ...plus all the other usual fields
}

@arturmuller
Copy link
Collaborator

This has been resolved in 2.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants