cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

Fail to load TS on node >= 16.18 #139

Closed ShlomiAltostra closed 1 year ago

ShlomiAltostra commented 1 year ago

Trying to compile code on nodejs version 16.18 or above fails with the following error:

/home/myself/repos/that-repo/node_modules/ttypescript/lib/loadTypescript.js:29
        var _e = ts.versionMajorMinor.split('.'), major = _e[0], minor = _e[1];
                                      ^

TypeError: Cannot read properties of undefined (reading 'split')
    at Object.loadTypeScript (/home/myself/repos/that-repo/node_modules/ttypescript/lib/loadTypescript.js:29:32)
    at Object.<anonymous> (/home/myself/repos/that-repo/node_modules/ttypescript/lib/tsc.js:8:27)
    at Module._compile (node:internal/modules/cjs/loader:1165:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)
    at Module.load (node:internal/modules/cjs/loader:1043:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1067:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/myself/repos/that-repo/node_modules/ttypescript/bin/tsc:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1165:14)

As much as I investigated - this is due to a change in how the Module object is constructed. When the TypeScriptModule constructor runs, the this object already has own-property named exports initialized with an empty object {}.

That property shadows the class property, preventing its getter from running, and the call to _init

In my setup, adding delete this.exports to the constructor fixes the problem as it deletes that own-property, exposing the class property again - but this wasn't tested thoroughly image

dmregister commented 1 year ago

Make sure you on the latest version: 1.5.15 https://github.com/cevek/ttypescript/releases

There were changes that addressed this issue: https://github.com/cevek/ttypescript/compare/1.5.12...1.5.14#diff-8fb112b766642acf9d525edac469bce302368688118ae8349238b7ff0606f328R32

ShlomiAltostra commented 1 year ago

I'm sorry I've ran npm i ttypescript at first, which didn't resolve the problem

I should've run npm i ttypescript@latest