-
Notifications
You must be signed in to change notification settings - Fork 94
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
Error [ERR_REQUIRE_ESM]: require() of ES Module #268
Comments
I faced same error using |
I'm not sure, but I think the bug is related to this PR: #263 |
Hi! This looks like an issue with how your TSC is configured. It likely outputs CJS code that contains I don't directly spot anything in your The whole CJS -> ESM story is a mess, and the fact that TypeScript uses a syntax that looks like ESM doesn't make things easier. Please make sure to post your solution in this issue, i'm pretty sure a lot of others will run into similar issue! |
I think it is related to the fact that there is no For Node16 the recommendation is at least Have you considered specifying |
@peterbud I don't think it's related to SuperJSON's |
Hi @Skn0tt , thanks for looking into this. There is no syntactical problem with the code which Consider the following: With the current ES3 it is transpiled to: simpleTransformation(isSet, 'set',
// (sets only exist in es6+)
// eslint-disable-next-line es5/no-es6-methods
function (v) { return __spreadArray([], __read(v.values())); }, function (v) { return new Set(v); }), Where var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
...
} leading to a warnings like If you compile with target 'es2020' (with typescript 4.2 that's the best you can use), you get a much better transpiled javascript dialect for the same function: simpleTransformation(isSet, 'set',
// (sets only exist in es6+)
// eslint-disable-next-line es5/no-es6-methods
v => [...v.values()], v => new Set(v)), If |
One more thing: That also specifies |
Good points! |
Thanks @Skn0tt - this has fixed the situation: warnings are gone! Thanks! |
Can you please release a CJS export? |
ESM is the standard format, has been supported by current Node.js versions for years. We need good reasons to add a CJS export again. If you feel your usecase has specific requirements and is important enough for SuperJSON to revisit its bundling, please outline that and open a separate issue. |
You're already using TypeScript which isn't a standard, so your bundler could export CJS format pretty easily. Then just update the |
Exporting multiple variants would be the most constructive for everyone and can happen seamlessly. Here would be an example of how to do that with TypeScript. |
I appreciate the effort in making your point @queicherius! My thoughts on the issue are very much the same that Jimmy Warting mentions here: node-fetch/node-fetch#1263 (comment) In short, I want the transition period between CJS and ESM to be over, and making packages ESM-only is a good way of doing that. For specific issues that arise from this, i'm happy to help finding workarounds in separate issues. |
I have the same problem, I'm unable to use this library. I have a typescript project with Express and trpc and I wanted to use this lib (as suggested from trpc). But after wasting hours, I wasn't able to fix this, the whole argument of commonJS and ESM for me is always cryptic and it's not something that I would like to dig in. {
"extends": "@tsconfig/node16/tsconfig.json",
"version": "4.4.2",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./lib",
"strictPropertyInitialization": false,
"target": "ESNext",
"module": "nodenext",
"moduleResolution": "nodenext"
},
"include": ["**/*.ts"],
"exclude": ["lib", "src/**/*.spec.ts", "node_modules"],
"ts-node": {
"files": true
}
} I've tried everything that make sense to me (esModuleInterop true) changing target/module/moduleResolution, but with no results. |
I +1 others' comments on the pain and difficulty here. But if helpful to others, here's how I got a // HACK: The `superjson` library is ESM-only (does not support CJS), while our codebase is CJS.
// This is a workaround to still get to use the latest version of the library from our codebase.
// https://github.com/blitz-js/superjson/issues/268
// https://www.npmjs.com/package/fix-esm
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const fixESM = require("fix-esm");
// @ts-expect-error This is a type-only import, so won't get transformed to `require()`.
import type SuperJSON from "superjson";
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment
const SuperJSON: SuperJSON = fixESM.require("superjson"); |
Would really appreciate a cjs export, its currently not possible for me to use a esm node server |
Same, we are forced to use 1.13.3 for now. |
i was toying around with a super basic express/trpc/superjson setup, and for HOURS i could not figure out why none of it was working. I tried changing my config in 1000 different ways, and none of it worked, except for @aseemk 's answer!!!! just, than you, so much !!!!!!! |
In response to @Skn0tt statement
After reviewing my tsconfig.json file, I made a small change to the moduleResolution option as shown below: {
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"incremental": false,
"isolatedModules": true,
"lib": ["es2022", "DOM", "DOM.Iterable"],
"module": "NodeNext",
"moduleDetection": "force",
- "moduleResolution": "node",
+ "moduleResolution": "NodeNext",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2022"
}
} |
I think it is too soon to make this change. A lot of packages and codebases are still locked in cjs and it is not possible to use NodeNext due to backwards compatibility, which is exactly my case. I'm locked in v1 because of this. Please provide cjs. <3 |
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
export const SuperJSON = require('fix-esm').require('superjson') as typeof import('superjson') |
Omg! I've trying to fix this for an entire year lol it was so hard to find this thread!!! Many thanks @aseemk your solution worked So the whole trpc.ts file looks like this:
|
Still facing this issue on a shared lib inside of a monorepo using nx... |
Version 1.13.3: Works fine.
Version 2.0.0: Din't work.
I'm using TRPC in my application (Electron + Vite + Vue + Typescript), according to the TRPC documentation I need to install
superjson
to transport data of types:Date/Map/Set
. However, after installing the"superjson": "^2.0.0"
lib, I am getting this error when running the application.tsconfig.json:
Uso:
The text was updated successfully, but these errors were encountered: