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.

Support setupProxy.mjs

See original GitHub issue

Is your proposal related to a problem?

Now as Node.JS 13.2 un-prefixed ES 6 Modules, I’d like to use ES 6 imports in my setupProxy.mjs, but CRA forces me to use setupProxy.js.

Describe the solution you’d like

Next to setupProxy.js, allow to use setupProxy.mjs (or setupProxy.cjs).

See also https://nodejs.org/api/esm.html#esm_enabling.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:8

github_iconTop GitHub Comments

2reactions
kenlyoncommented, Apr 22, 2022

I have a workaround. It’s pretty gross, but it got me unstuck today.

My app is using ESM as the default format. My /package.json has "type": "module".

When I made the change, the react-app-rewired build complained about /src/setupProxy.js being ESM so I changed it to .cjs and thought I was done. Only today (weeks later) did I notice that /src/setupProxy.cjs is ignored.

Here’s the fix for defining your own custom setupProxy logic in an ESM world:

  1. Create a folder called /src/setupProxy.js.
  2. Move your old setupProxy content into a new file called /src/setupProxy.js/index.js. The content of this file must be in CommonJS format.
  3. Create a file called /src/setupProxy.js/package.json that contains the text { "type": "commonjs" }. This ensures the neighbouring file is loaded as CommonJS, so it will work when it’s “require’d” in.

So, to recap - your structure should now be:

  • src
    • setupProxy.js (a directory!)
      • index.js
      • package.json

It’s not pretty, but it works!

Thanks to @eric-parsons for the idea of a directory with the .js extension. That was the key!

0reactions
RThevenouxcommented, Jun 24, 2022

@kenlyon Thank you very much ! I don’t know if there is a fix for this problem, but your solution save my day !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proxy to backend with default Next.js dev server - Stack Overflow
Before, when I made apps with create-react-app, I would have a setupProxy.js file that would route API requests similar to this
Read more >
setupProxy.js does not work inside React App
Hello, I am having a setupProxy.js file under the src/ folder to deal ... and it doesn't seem to support react or any...
Read more >
Migrating from 1.x to 2.0.3 - create-react-app - Breword 文档集合
Object proxy configuration is superseded by src/setupProxy.js ... It's worth highlighting: if your proxy field is a string , e.g. http://localhost:5000 , or...
Read more >
Monorepo Setup for Create React App and Components (2/4)
If need help on how to initialize a monorepo, have a look into part 1. ... dist/index.js" has to be .js - using...
Read more >
What's new in Create React App 2.0 ? - GeeksforGeeks
Babel: Browser support for modern features of JavaScript has ... your React application right inside the source folder called setupProxy.js ...
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