Repository: create-react-app
Issue: Create React App (CommonJS, ESNext) + Path Alias + Module (Import/Export) + NodeJS + TSConfig.json
Labels: stale,needs triage,issue: bug report

## Overview
Stop... Slow... Down... and Chill... 😅  if you're having a problem with your CRA module import/export syntax or path aliases, after upgrading either `react-scripts`, `typescript`, or `nodeJS` look no further, I've spent hours going through and consolidating and organizing the information to resolve your issue you're welcome 😄 to thank me later.

## Problem
It all simply boils down to this... [CommonJS vs ES6](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1)  CRA wants ES6 and Typescript wants CommonJS

## Requirements
This repo gives you a fresh install of the latest [CRA -  ReactScripts](https://github.com/facebook/create-react-app#readme) + [TypeScript](https://www.typescriptlang.org/), make sure you update or download the latest version of [NodeJS](https://nodejs.org/en/download/) (currently 14.17.0) 

```js
git clone https://github.com/jdavis-software/latest-cra-ts-node-example
```

## Dependencies

|     Package           |Version                          
|----------------|-------------------------------|
|react-scripts     |`<=4.0.3`            
|typescript          |`<=4.2.4`        
|node                   | `<=14.17.0`
## Bug Description
After upgrading my packages to `react-scripts@4.0.3` and `typescript@4.2.4` I'm receiving this error:

``` js
ReferenceError: Cannot access { myFunction } before initialization
```

This is related to exporting function expressions during declaration like this example:

``` js
export const myFunction = () => {  /* do something */  }
```

Most of the application is written in this shorthand fat arrow function expression syntax, and this has always worked previously in the past before this update. After doing more research it looks like this problem is related to the [module](https://www.typescriptlang.org/tsconfig#module) option inside [compilerOptions](https://www.typescriptlang.org/tsconfig) declared in the `tsconfig.json` file. 

## Problem Overview
The problem seems to be with the following [esnext](https://www.typescriptlang.org/tsconfig#commonjs) value inside the `module` option:

```js
"compilerOptions": {
    "module": "esnext"
}
```

Now changing this back to [commonjs](https://www.typescriptlang.org/tsconfig#commonjs) should resolve this problem, however after updating the option to the following:

```js
"compilerOptions": {
    "module": "commonjs"
}
```
Then running `npm run start`, you would think this would work, however, CRA/Webpack goes into the `tsconfig.json` file and reverts it back to the previous `esnext` module, thus again failing and causing the reference error: 

``` js
ReferenceError: Cannot access { myFunction } before initialization
```

Does anyone have a solution, to allow export declarations of fat arrow function expressions, yet?

``` js
export const myFunction = () => { /* do something */ }
```

## Useful Reference Links That May Help You!

- [TSConfig Compiler Options](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
- [TSConfig `extends` base overides](https://github.com/tsconfig/bases)
- [TypeScript Declartion Files (.d.ts)](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html)
- [CommonJS vs ES6](https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1)

## Keywords
- Module not found: Can't resolve
- Cannot access before initialization
- Create React App CommonJS
- Create React App changes tsconfig
- Create React App updates tsconfig
- Create React App removes commonJS
- Create React App exports error
- Create React App imports
- Create React App module imports
- Create React App module exports

## Appreciate
If this helped you and save you time... feel free to send (BAT) - Basic Authentication Tokens... I need more coffee and dinners 😅 

BAT Token Address - 0x1A62e25073a9038647c235509b4349988eED19Ae

<img width="355" alt="Screen Shot 2021-05-20 at 7 20 51 PM" src="https://user-images.githubusercontent.com/21320194/119072608-9de70100-b9a0-11eb-9cb3-099bb2f639f7.png">

Anyways wanted to spread peace, love and happiness to everyone. I love what I do and I hope everyone reading this is having a great day, take care guys!