question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setupProxy.js not working together with TypeScript

See original GitHub issue

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”.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
brunolemoscommented, Oct 26, 2018

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.

1reaction
antonovichacommented, Nov 28, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Proxy in Create React App using Typescript
I got it working with vanilla javascript, but am unable to get it to work with TypeScript. Has anyone gotten setupProxy to work...
Read more >
setupProxy.js does not work inside React App
Hello, I am having a setupProxy.js file under the src/ folder to deal with Cross-Origin Issues from localhost. However, it doesn't work and ......
Read more >
Why you should use a proxy server with Create React App
Using a manually created proxy in Create React App​​ json file, then create a new file named setupProxy. js in the src directory....
Read more >
not working: http-proxy-middleware test - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >
Developing and deploying create-react-app to multiple ...
I was running into issues when setting up proxy (in src/setupProxy.js). I went through the changelog for http-proxy-middleware and found ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found