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

TypeError: __require.resolve is not a function #369

Closed
mvrlin opened this issue Jul 25, 2021 · 10 comments
Closed

TypeError: __require.resolve is not a function #369

mvrlin opened this issue Jul 25, 2021 · 10 comments

Comments

@mvrlin
Copy link

mvrlin commented Jul 25, 2021

Hello! I have an issue with the tsup, I have a TypeScript project with require.resolve function, with pure tsc it works, but when I try to build with tsup-node I get this error:

TypeError: __require.resolve is not a function

I made a reproduction repo for this issue:
https://github.com/mvrlin/tsup-require-resolve

I came across this "solution":

// Before
var __require = (x) => {
  if (typeof require !== "undefined")
    return require(x);
  throw new Error('Dynamic require of "' + x + '" is not supported');
};

// After
var __require = (x) => {
  throw new Error('Dynamic require of "' + x + '" is not supported');
}

if (typeof require !== "undefined") {
  __require = require
}
@hyrious
Copy link

hyrious commented Jul 28, 2021

The __require is emitted by esbuild if the format is NOT cjs. I think cli-node.ts should add a flag to do that.

Your "solution" about __require is a good advice. I think you can post it to esbuild.

@anymaniax
Copy link

Hello! I had the same problem at the moment I just disable the code splitting to have the code working. Did you have another solution?

@screetBloom
Copy link
Contributor

screetBloom commented Sep 8, 2021

Share my solution

"scripts": {
-  "build": "tsup"
+  "build": "tsup && node fix.js"
}
// fix.js
const beforeStr = `var __require = (x) => {
  if (typeof require !== "undefined")
    return require(x);
  throw new Error('Dynamic require of "' + x + '" is not supported');
};`
const replacedContent = `
  ${beforeStr}
  if (typeof require !== "undefined") {
    __require = require
  }
`

const res = str.replace(beforeStr, replacedContent)

@screetBloom
Copy link
Contributor

screetBloom commented Sep 8, 2021

After reading the source code of tsup, I found that there is workaround

"scripts": {
-  "build": "tsup"
+  "build": "tsup --format cjs --no-splitting"
}

Using --format alone won't work, you need --no-splitting

Then the code can go to output as expected

@hyrious
Copy link

hyrious commented Sep 8, 2021

@screetBloom Yes that should work, but it will lose code splitting.

@screetBloom
Copy link
Contributor

@screetBloom Yes that should work, but it will lose code splitting.

Yes, but code splitting in esbuild must be esm


When we userequire, it's usually in the node side

@screetBloom
Copy link
Contributor

But admittedly, esbuild's default __require does need to be fixed

@screetBloom
Copy link
Contributor

screetBloom commented Sep 8, 2021

Also I think splitting should be false when format === 'cjs', And should be enabled by default when format is esm

@screetBloom screetBloom mentioned this issue Sep 8, 2021
7 tasks
@hyrious
Copy link

hyrious commented Sep 10, 2021

Since esbuild 0.12.26, this issue should be solved automatically by updating dependencies. (npm update)
@mvrlin Can you confirm that?

@mvrlin
Copy link
Author

mvrlin commented Sep 10, 2021

@hyrious Yes, working as expected! Thank you!

@mvrlin mvrlin closed this as completed Sep 10, 2021
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

No branches or pull requests

4 participants