react-native-paper: App fails to compile with react-native-web
I’m trying to use react-native-paper in conjunction with react-native-web. React-native-web is an npm package which aliases react-native, and allows you to run react-native apps in the browser (ie: no simulator). It renders components as android, and it uses react-native v0.55.
Current behaviour
When I run the app without using react-native-paper, it runs fine. When I use react-native-paper however, I currently get an error message (see below) and the app fails to compile.
Expected behaviour
I expect for the app to run as a react-native-web app, with* react-native-paper* providing components.
Below is the error message I receive when I run npm start
.
Failed to compile
./node_modules/react-native-paper/src/index.js
SyntaxError: /media/binyamin/Seagate Expansion Drive/Documents/Coding/react-native/my-web-app/node_modules/react-native-paper/src/index.js: Unexpected token (4:12)
2 |
3 | import * as Colors from './styles/colors';
> 4 | import type { Theme as _Theme } from './types';
| ^
5 |
6 | export type Theme = _Theme;
7 |
Code sample
Github Repo: https://github.com/b3u/my-web-app
Just run npm start
to reproduce the issue
Screenshots (if applicable)
What have you tried
1. Search Google
2. Search existing issues
3. Create a custom theme
4. Opt out of using icons
5. Downgrade to v2.1.3
Your Environment
software | version |
---|---|
ios or android | n/a (the web app displays as android) |
react-native | react-native-web v0.9.8 |
react-native-paper | v2.2.8 |
node | v10.13 |
npm or yarn | npm v6.4.1 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 45 (18 by maintainers)
We’re planning to publish already transpiled code to NPM so the babel stuff won’t be necessary, but yea, we’ll add a guide to use with react-native-web soon.
@satya164 Hi!
I got the same error as @b3u, but in my case I used expo to create my base app to use react-native-web.
Is there any specific configuration to webpack from expo?
here is my babel.config.js
If you are using https://facebook.github.io/create-react-app. You can use https://github.com/timarney/react-app-rewired to config webpack without
react-scripts eject
.$ yarn add --dev babel-loader @babel/preset-env @babel/preset-react @babel/preset-flow @babel/plugin-proposal-class-properties @babel/plugin-proposal-object-rest-spread
$ yarn add --dev react-app-rewired
And try run your application.
I can describe how to get react-native-paper working in expo.
expo init --yarn --template blank demo-app
–
cd demo-app
yarn add react-native-web react-router-dom react-router-native react-app-polyfill react-art react-native-paper react-dom
–
yarn add -D react-scripts @babel/preset-flow @babel/plugin-proposal-class-properties
code package.json
and add scripts:“web”: “react-scripts start”, “build-web”: “react-scripts build”
– we’re going to be cheating and editing them in-place. A better practice is to copy node-modules/react-scripts/ config and scripts into your project folder, install their dependencies and get them working locally. But this is just a proof-of-concept (so … just be sure not to reinstall node_modules or react-scripts for now on)
– configure/add main:
“main”: “react-native-main.js”,
code react-native-main.js
saving:mkdir src public
rm App.js
–
code src/App.js
saving:mkdir src/Controllers && code src/Controllers/Home.js
saving: (need to make something to demo Paper … this is essentially just the text example from the examples folder)code public/index.html
saving:code src/index.js
saving:code src/routing.native.js
saving:export { NativeRouter as Router, Switch, Route, Link } from ‘react-router-native’
–
code src/routing.web.js
saving:export { BrowserRouter as Router, Switch, Route, Link } from ‘react-router-dom’
yarn ios
should work butyarn web
gives the error reported here. We need to edit the react-scripts Webpack confignode_modules/react-scripts/config/webpack.config.js
:– to the plugins of the section marked:
(at about line 387) add this plugin:
after that section, create a new section:
When I run
npm run build
, I get the following error: