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

ReferenceError in strict mode #3882

Closed
wdzeng opened this issue Aug 16, 2024 · 2 comments
Closed

ReferenceError in strict mode #3882

wdzeng opened this issue Aug 16, 2024 · 2 comments

Comments

@wdzeng
Copy link

wdzeng commented Aug 16, 2024

I found cjs files generated from ts files sometimes throws ReferenceError. I found the problem with a project using adm-zip library.

Reproduction

Using Node.js v20.15.0.

Steps

  1. package.json
{
  "dependencies": {
    "@types/adm-zip": "0.5.5",
    "adm-zip": "0.5.15",
    "esbuild": "0.23.0"
  }
}
  1. tsconfig.json
{
  "compilerOptions": {
    "strict": true
  }
}
  1. test.ts
import AdmZip from 'adm-zip'
const zip = new AdmZip()
zip.addFile('test.txt', Buffer.from('hello world'))
zip.writeZip('/tmp/test.zip')
  1. Run npm i && npx esbuild test.ts --platform=node --target=node20 --bundle --outfile=/tmp/test.cjs && node /tmp/test.cjs

Expected Result

/tmp/test.cjs generates /tmp/test.zip.

Actual Result

/tmp/test.cjs throws ReferenceError and fails.

ReferenceError: totalEntries is not defined
    at Object.compressToBuffer (/tmp/test.cjs:1790:24)
    at Object.writeZip (/tmp/test.cjs:2629:30)
    at Object.<anonymous> (/tmp/test.cjs:2688:5)

Hints

  1. Running node /path/to/the/following/script.js will succeed.
"use strict";
const AdmZip = require('adm-zip')
const zip = new AdmZip()
zip.addFile('test.txt', Buffer.from('hello world'))
zip.writeZip('/tmp/test.zip')
  1. If we remove "strict": true in the tsconfig.json, the reproduction will give the expected result.
@hyrious
Copy link

hyrious commented Aug 16, 2024

I believe this issue is due to adm-zip itself: cthackers/adm-zip#526

esbuild emits "use strict"; on top of the out file if it sees strict config in tsconfig.json. If you delete the first line of /tmp/test.cjs, the issue would disappear because in sloppy mode you just can assign to any global variable without a declaration.

@wdzeng
Copy link
Author

wdzeng commented Aug 16, 2024

Oh thanks!

@wdzeng wdzeng closed this as completed Aug 16, 2024
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

2 participants