Module parse failed: Unexpected token. react-native/index.js "typeof" operator

IurKo

I'm quite new to the front end - trying to implement client side on React. After adding "react-native" dependency and running webpack I'm getting the following error:

ERROR in ./node_modules/react-native/index.js 13:7
Module parse failed: Unexpected token (13:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 'use strict';
| 
> import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo';
| import typeof ActivityIndicator from './Libraries/Components/ActivityIndicator/ActivityIndicator';
| import typeof Button from './Libraries/Components/Button';
 @ ./node_modules/@react-navigation/native/lib/module/useBackButton.js 2:0-43 5:25-36
 @ ./node_modules/@react-navigation/native/lib/module/index.js
 @ ./src/main/js/app.js

I have the following dependencies in package.json:

 "dependencies": {
    "@react-native-community/masked-view": "^0.1.7",
    "@react-navigation/native": "^5.1.4",
    "@react-navigation/stack": "^5.2.9",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-native": "^0.62.1",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "^1.7.1",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.4.0",
    "rest": "^2.0.0"
  },
  "scripts": {
    "watch": "webpack --watch -d"
  },
  "devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/preset-react": "^7.9.4",
    "babel-loader": "^8.1.0",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11"
  }

I assume that "typeof" operator is not recognized, but why?

Blundering Philosopher

I just spent a few hours dealing with this exact issue.

First, you can try adding "@babel/preset-flow" to your .babelrc file (after installing). That was recommended here, but didn't actually work for me.

What worked for me was just adding externals: { "react-native": true }, to my webpack.config.js file. My config file ended up looking like this:

const path = require("path")

module.exports = {
    entry: "./src/index.js",
    output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
    },
    externals: {
        "react-native": true,
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: ["babel-loader"],
            },
            // ...
        ],
    },
}

My understanding is that react-native is a dependency, so @babel/preset-flow didn't actually get triggered to help interpret the flow formatting in those files - it may only handle files in the main "entry" location of your webpack.config.js file.

Hopefully this helps someone out there. Feel free to comment on this answer if I'm a bit off-base and I'll update this :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Module parse failed: Unexpected token

From Dev

React JS: ERROR in index.css 1:0 (Module parse failed: Unexpected token (1:0))

From Dev

Webpack 4 basic React js hello world fails with "Module parse failed: Unexpected token"

From Dev

React and Webpack Module parse failed: /testimonials.js Unexpected token (6:4)

From Dev

Module parse failed: Unexpected token on React Component Render

From Dev

How to fix Module parse failed: Unexpected token?

From Dev

webpack tsx Module parse failed: Unexpected token

From Dev

Module parse failed: Unexpected token ionic 3

From Dev

Module parse failed: Unexpected token (7855:112)

From Dev

Webpack failed to parse TypeScript module: Unexpected token

From Dev

Vue Module parse failed: Unexpected token

From Dev

Module parse failed: Unexpected token (11:19)

From Dev

React Module parse failed: Unexpected character '@'

From Dev

Module parse failed: Unexpected token (6:16) when building react with webpack

From Javascript

React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type

From Dev

React / Webpack - "Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type."

From Dev

How can I solve the Module parse failed: Unexpected token (11:9) react-router-native

From Dev

Module parse failed: Unexpected token (1:0) vue.js vuex store

From Dev

vue js serve problem - Module parse failed: Unexpected token (763:13)

From Dev

Module parse failed: Unexpected token when using webpack 5

From Dev

Webpack error after upgrading Node: "Module parse failed: Unexpected token"

From Dev

Module parse failed: Unexpected token using webpack-dev-server

From Dev

Module Parse Failed: Unexpected token in Webpack Typescript Loader

From Dev

Module parse failed: Unexpected token (9:37) with babel-loader

From Dev

Module parse failed: Unexpected token (14:6) while heroku deployment

From Dev

CSS Module parse failed: Unexpected token Angular 14?

From Dev

ReactJS - Module build failed: SyntaxError: Unexpected token react components not rendering

From Dev

Webpack module build failed unexpected token (rails react build)

From Dev

Webpack, React JSX, Babel: Module build failed, "unexpected token" empty?

Related Related

  1. 1

    Module parse failed: Unexpected token

  2. 2

    React JS: ERROR in index.css 1:0 (Module parse failed: Unexpected token (1:0))

  3. 3

    Webpack 4 basic React js hello world fails with "Module parse failed: Unexpected token"

  4. 4

    React and Webpack Module parse failed: /testimonials.js Unexpected token (6:4)

  5. 5

    Module parse failed: Unexpected token on React Component Render

  6. 6

    How to fix Module parse failed: Unexpected token?

  7. 7

    webpack tsx Module parse failed: Unexpected token

  8. 8

    Module parse failed: Unexpected token ionic 3

  9. 9

    Module parse failed: Unexpected token (7855:112)

  10. 10

    Webpack failed to parse TypeScript module: Unexpected token

  11. 11

    Vue Module parse failed: Unexpected token

  12. 12

    Module parse failed: Unexpected token (11:19)

  13. 13

    React Module parse failed: Unexpected character '@'

  14. 14

    Module parse failed: Unexpected token (6:16) when building react with webpack

  15. 15

    React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type

  16. 16

    React / Webpack - "Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type."

  17. 17

    How can I solve the Module parse failed: Unexpected token (11:9) react-router-native

  18. 18

    Module parse failed: Unexpected token (1:0) vue.js vuex store

  19. 19

    vue js serve problem - Module parse failed: Unexpected token (763:13)

  20. 20

    Module parse failed: Unexpected token when using webpack 5

  21. 21

    Webpack error after upgrading Node: "Module parse failed: Unexpected token"

  22. 22

    Module parse failed: Unexpected token using webpack-dev-server

  23. 23

    Module Parse Failed: Unexpected token in Webpack Typescript Loader

  24. 24

    Module parse failed: Unexpected token (9:37) with babel-loader

  25. 25

    Module parse failed: Unexpected token (14:6) while heroku deployment

  26. 26

    CSS Module parse failed: Unexpected token Angular 14?

  27. 27

    ReactJS - Module build failed: SyntaxError: Unexpected token react components not rendering

  28. 28

    Webpack module build failed unexpected token (rails react build)

  29. 29

    Webpack, React JSX, Babel: Module build failed, "unexpected token" empty?

HotTag

Archive