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
git clone git@github.com:bryanterrell/hello-next-flow.gitcd hello-next-flownpm inpm run dev- Open http://localhost:3000 in browser & click Go to About Page
- 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)
I did some investigating, and I think the root cause of this is that next.js is using an older version of
@babel/corewhich uses the archivedbabyloninstead of@babel/parser.This line in plugin-syntax-flow sets
parserOptions.pluginsto[["flow", { all }]].This is fine with the latest
@babel/core, because it expects arrays withinparserOptions.pluginsaccording 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
resolutionsonpackage.jsonto:{ "babel-core": "7.0.0-beta.53" }, which uses@babel/coreas its parser rather thanbabylon, 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
babeldependencies ofnexttobeta.53- but that might make some headway to fixing this!There seems to be a regression between the
beta.47versions of the@babelpackages and thebeta.48versions that stops Babel from parsing flow’s types. Trying to investigate further but reverting back tobeta.47will 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