rolldown icon indicating copy to clipboard operation
rolldown copied to clipboard

Should use custom `require` while bundling code `require('external')`

Open hyf0 opened this issue 1 year ago • 0 comments

Esm code that contains require(...) will throw errors like

console.log(fs, nodeFs, require('path'), require('node:path'));
                ^

ReferenceError: require is not defined in ES module scope, you can use import instead

Esbuild replaces require with __require to prevent such problems while bundling. See

https://hyrious.me/esbuild-repl/?version=0.21.3&b=e%00entry.js%00import++as+fs+from+%22fs%22%3B%0Aimport++as+nodeFs+from+%22node%3Afs%22%3B%0A%0A%2F%2F+main.js%0Aconsole.log%28fs%2C+nodeFs%2C+require%28%27path%27%29%2C+require%28%27node%3Apath%27%29%29%3B%0A&b=%00file.js%00%0Aconst+a+%3D+1%3B%0Aexport+%7B+a+%7D%0A%0A&b=%00file2.js%00console.log%282%29%0Aexport+const+b+%3D+1&o=--bundle+--format%3Desm+--splitting+--outdir%3D.+--tree-shaking%3Dfalse+--platform%3Dnode

hyf0 avatar May 21 '24 10:05 hyf0