Create-react-app: setupProxy.js doesn't support Flow

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

Is this a bug report?

Yes.

Did you try recovering your dependencies?

Yes.

Which terms did you search for in User Guide?

"proxy" and "setupProxy"

Steps to Reproduce

  1. make a src/setupProxy.js with Flow annotations (the ": *") like,

    // @flow
    const proxy = require('http-proxy-middleware');
    module.exports = function(app: *) {
      app.use(proxy('/api', { target: 'http://localhost:3001/' }));
    };
    
  2. run yarn start

Expected Behavior

The usual dev server startup

Actual Behavior

    $ yarn start
    yarn run v1.7.0
    $ react-scripts start
    Unexpected token :
    error Command failed with exit code 1.
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
documentation

All 5 comments

This is the expected behavior. This file runs in the Node environment and as such, only supports its syntax.

df98c0c5611174266a7161fa716a142791eea13b

I stumbled upon this issue #5185 because I was experiencing the exact same "Actual Behavior". What fixed things for me was changing my setupProxy.js file from this:

# setupProxy.js

import proxy from 'http-proxy-middleware';

export default function(app) {
  app.use(proxy('/api', { target: 'http://localhost:5000/' }));
};

to this:

# setupProxy.js

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

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

I'm commenting incase it saves someone else time in the future.

@RodneyU215 that proxy seems pretty simple, does the normal proxy string option not work for your use case?
Don't mind me if it was a reduced case.

Hi @Timer! Yes the normal proxy string does work for this use case. I was merely highlighting the incompatibility with es2015 module syntax.

Was this page helpful?
0 / 5 - 0 ratings