next.js: Flow Type Error with Next.js 6

Bug report

Describe the bug

When components with flow types are used the pages using those components throw an “Unexpected token” error due to type.

Packages:

  • babel/preset-flow
  • flow-bin

.babelrc

  • babel/preset-flow in in presets
  • babel/plugin-transform-flow-strip-types in plugins.

To Reproduce

  1. git clone git@github.com:bryanterrell/hello-next-flow.git
  2. cd hello-next-flow
  3. npm i
  4. npm run dev
  5. Open http://localhost:3000 in browser & click Go to About Page
  6. Error is produced:
Syntax Error: Unexpected token, expected ";" (3:5)

  1 | import * as React from "react";
  2 |
> 3 | type Props = {
    |      ^
  4 |   foo: number,
  5 |   bar?: string
  6 | };

Expected behavior

Flow types should be stripped out so component / page can be rendered.

System information

  • OS: macOS
  • Next.js version: 6.0.3

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 19
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I did some investigating, and I think the root cause of this is that next.js is using an older version of @babel/core which uses the archived babylon instead of @babel/parser.

This line in plugin-syntax-flow sets parserOptions.plugins to [["flow", { all }]].

This is fine with the latest @babel/core, because it expects arrays within parserOptions.plugins according to this line.

But in babylon, this check against a simple string instead of an array will mean that flow isn’t recognised as a plugin, and therefore the parser that can understand flow syntax will not be loaded.

Setting resolutions on package.json to: { "babel-core": "7.0.0-beta.53" }, which uses @babel/core as its parser rather than babylon, does fix the problem of parsing Flow types (but creates some new errors!)

@timneutkens I don’t know if the fix for this is as simple as updating the babel dependencies of next to beta.53 - but that might make some headway to fixing this!

There seems to be a regression between the beta.47 versions of the @babel packages and the beta.48 versions that stops Babel from parsing flow’s types. Trying to investigate further but reverting back to beta.47 will solve the issue for now!

Still encountering this issue

I still had issues with using graphiql, but was able to fix it with this: https://github.com/ericclemmons/customized-graphiql/blob/master/next.config.js

@oliviertassinari i got it working, you have to install @babel/cli and @babel/core