babel-plugin-inline-react-svg: Duplicate declaration "React"
I’m using this plugin with next.js, which uses babel-plugin-react-require
. It seems that the changes in 3bab74c5587d33892fd2cb409df2ae3fad641b6e are conflicting with that.
So now I get this:
Module build failed: TypeError: unknown: Duplicate declaration "React"
5 | import _inherits from 'babel-runtime/helpers/inherits';
6 | import React from 'react';
> 7 | import React from 'react';
| ^
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 9
- Comments: 33
The
next/babel
preset includes https://unpkg.com/next@4.1.4/dist/server/build/babel/preset.js which usesbabel-plugin-react-require
- which has a bug, as documented above.This issue should be closed, and an issue filed on https://github.com/vslinko/babel-plugin-react-require instead.
I downgraded to v0.4.0 and this solved the problem. Not sure if the plugin is working well yet though
@ljharb I just published the PR to babel-plugin-react-require: https://github.com/vslinko/babel-plugin-react-require/pull/18
This issue can be closed now, as it’ll be out on Next.js soon.
🙌
@timneutkens this issue seems to be resurfacing with
next@8.1.0
, asbabel-plugin-react-require@3.0.0
is listed as a dependency whereas the fix is inbabel-plugin-react-require@3.0.1
see comments here: https://github.com/zeit/next.js/pull/6281
not sure how to go about solving this apart from manually adding an
import React
statement?@ljharb It only happens when there’s no pre-existing React import, which is why it has been surfacing only for those using
babel-plugin-react-require
, I presume.Ran into the same issue a few minutes ago, resolved by downgrading to 5.1.
Was the kind of error I was receiving on all files that imported an SVG file. This issue happened once I migrated to webpack 4.
It still doesn’t work with this config:
However it seems the
babel-plugin-react-require
is not required by nextjs anymore. Removing it and importing the React package manually on each file solves the problem. 😃@ljharb
The order doesn’t matter, preset plugin visitors are always chained after user-supplied plugin visitors, at least until plugin ordering gets revamped (starting with https://github.com/babel/babel/pull/5735).
It shouldn’t. As mentioned by @christophehurpeau, this plugin adds a React import per each SVG, without any involvement from
babel-plugin-react-require
. Consider the following fixture:It results in the following output:
This could be fixed by having a
hasReactBeenEnsured
flag inProgram.enter
, but, IMO, this plugin shouldn’t be adding a React import in the first place, for the same reasons Babel itself isn’t adding a React import when it transforms JSX, as outlined in https://github.com/babel/babel/issues/586 and https://github.com/babel/babel/issues/2135 and https://github.com/babel/babel/issues/2504.