This error is thrown by Vitest, when running tests against a project using a package compiled by tsup
.
TypeError: __require.resolve is not a function
The bundled code of lib/index.mjs
looks like this:
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined")
return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
Here's the tsup.config.json
that was used to build the library:
{
"clean": true,
"sourcemap": true,
"watch": false,
"silent": true,
"entry": [
"src/index.ts"
],
"dts": {
"resolve": true
},
"tsconfig": "tsconfig.build.json",
"outDir": "lib",
"format": [
"cjs",
"esm"
],
"shims": true,
"target": "node18.19.0"
}
I'm wondering why is that tsup
not shimming the require.resolve
correctly?
Adding this shim myself, before using require
works correctly:
const require_ = createRequire(
__filename ??
// @ts-expect-error Forward compat. The `import.meta` meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
import.meta.url,
)
Package versions:
tsup 8.0.2
βββ¬ bundle-require 4.0.2
β βββ esbuild 0.19.12 peer
βββ esbuild 0.19.12
tsup
issue that says it was fixed: #369esbuild
: evanw/esbuild#1921 (comment)Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too