microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 101 forks source link

Unexpected crash: dts-gen -m aws-amplify-react-native: "SyntaxError: Cannot use import statement outside a module" #152

Open dantasfiles opened 4 years ago

dantasfiles commented 4 years ago
> dts-gen -m aws-amplify-react-native
Unexpected crash! Please log a bug with the commandline you specified.
C:\...\node_modules\aws-amplify-react-native\dist\index.js:14
import { default as AmplifyCore, I18n } from 'aws-amplify';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1067:16)
    at Module._compile (internal/modules/cjs/loader.js:1115:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)       
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1040:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\Users\...\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:58:67)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)  
dantasfiles commented 4 years ago

The problem is require doesn't support ES6 modules. https://github.com/microsoft/dts-gen/blob/bff184105c86a8f7f9e2f4f9a424775cdd4f7c4c/lib/run.ts#L77

The solution in https://github.com/microsoft/dts-gen/issues/42#issuecomment-414038955 uses @babel/register:

The require hook will bind itself to node's require and automatically compile files on the fly.

However, it kept on wanting me to install more and more packages, aws-amplify-react-native -> aws-amplify -> react-native. Finally, it crashed with an Invariant Violation: __fbBatchedBridgeConfig is not set, cannot invoke native modules at invariant error.

This seems to be too complex a use-case for dts-gen so I'm going to give up for now.

ETA: I hit "close issue" by mistake.