sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
5.08k stars 161 forks source link

ReferenceError: Cannot access 'Object' before initialization #1063

Closed devuo closed 2 weeks ago

devuo commented 2 weeks ago

Affected version

0.33.20

Description

After updating to typebox version 0.33.20 CommonJS based package failed to start with the following error: ReferenceError: Cannot access 'Object' before initialization.

This error does not happen with ESM based packages.

Steps to reproduce

🔴 CommonJS (Fails):

echo "require('@sinclair/typebox')" > reproduce.js
node reproduce.js

🟢 ESM (Works):

echo "import '@sinclair/typebox'" > reproduce.mjs
node reproduce.mjs

Stack trace

~/typebox-test/node_modules/@sinclair/typebox/build/cjs/parse/runtime.js:3
Object.defineProperty(exports, "__esModule", { value: true });
^

ReferenceError: Cannot access 'Object' before initialization
    at Object.<anonymous> (~/typebox-test/node_modules/@sinclair/typebox/build/cjs/parse/runtime.js:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (~/typebox-test/node_modules/@sinclair/typebox/build/cjs/parse/parse.js:7:19)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)

Node.js v20.17.0
sinclairzx81 commented 2 weeks ago

@devuo Hi, thanks for reporting

There's been a bit of infrastructure added to the project on 0.33.20. Will investigate and report back.

sinclairzx81 commented 2 weeks ago

@devuo Hi, thanks again for reporting.

I have published a hotfix on 0.33.21. The issue was related to a variable named Object shadowing the globalThis.Object (I seem to keep running into this problem unfortunately). The variable has been prefixed with _Object inline with code elsewhere.

Apologies for the inconvenience (I really do need better CJS testing)


New Infrastructure

If you're interested, the new infrastructure that's gone in relates to being able to parse TypeScript syntax directly into TB types (instead of using Type.*). You can test with the following.

const { Parse } = require('@sinclair/typebox')

const A = Parse(`{ x: number, y: number }`)

console.log(A)

The feature is published in an Experimental state currently, but will be expanded upon in 0.34.x. The feature is currently pending documentation, but you can experiment at the link below, I'm currently looking for community feedback on the feature, so open to community insights, bug reports, feature requests to consider for the next main revision.

TypeScript Link Here

Thanks again for reporting the issue Cheers! S