Create-react-app: setupProxy.js not working together with TypeScript

Created on 26 Oct 2018  路  5Comments  路  Source: facebook/create-react-app

Is this a bug report?

Yes

Environment

CRA ^2.0.6-next.c662dfb0 with TypeScript support

Steps to Reproduce

  1. npx create-react-app --scripts-version @next myapp
  2. cd myapp
  3. yarn add typescript @types/react @types/react-dom @types/jest
  4. mv src/index.{js,tsx}
  5. Add src/setupProxy.js with following content:
const proxy = require('http-proxy-middleware')

module.exports = function(app) {
    app.use(proxy('/api', { target: 'http://localhost:5000/' }))
}
  1. yarn run build

Expected Behavior

setupProxy.js should be compiled without any problems.

Actual Behavior

Behold the "Type error: Cannot compile namespaces when the '--isolatedModules' flag is provided. TS1208" error message.

Additionally: if one were to rewrite the setupProxy.js script with proper ES6 exports like this:

const proxy = require('http-proxy-middleware')

export const setupProxy = (app) => {
    app.use(proxy('/api', { target: 'http://localhost:5000/' }))
}

yarn run build compiles successfully, while yarn run start claims "unexpected token export".

Most helpful comment

Thanks for the report!

You are right. For now, please add this line to your tsconfig.json:

"exclude": ["src/setupProxy.js"]

Let me know if that fixes the issue for you.

All 5 comments

Thanks for the report!

You are right. For now, please add this line to your tsconfig.json:

"exclude": ["src/setupProxy.js"]

Let me know if that fixes the issue for you.

Yes, that works for me.

JSON imports are also broken, exclude is not helping.

@Timer it does not work in 2.1.1 for us. Workaround is working fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  路  3Comments

rdamian3 picture rdamian3  路  3Comments

onelson picture onelson  路  3Comments

Aranir picture Aranir  路  3Comments

jnachtigall picture jnachtigall  路  3Comments