首页 > 解决方案 > SyntaxError:“@react-navigation”上的意外令牌“导出”

问题描述

我创建了一个新的 React Native 项目并使用了 Jest 测试,效果很好。但是当我在现有应用程序中尝试它时它无法工作,而是给了我以下错误。我怀疑这是导致此问题的配置错误:

错误

SyntaxError: Unexpected token 'export'

  4 | //Import React
  5 | import React, {useEffect, useState} from 'react';
> 6 | import { StackActions, NavigationActions } from 'react-navigation';
    | ^
  7 | import {useSelector, useDispatch} from 'react-redux';
  8 | import {changeVoucherCategory} from '../../../actions';
  9 | //Import all required component

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/Screen/drawerScreens/Profile/ProfileScreen.js:6:1)

以下是我的一些文件

包.json

{
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "react-scripts start",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "build:ios": "react-native bundle --entry-file='index.ios.js' --bundle-output='./ios/IGBFAMILY/main.jsbundle' --dev=false --platform='ios' --assets-dest='./ios'"
  },
  "dependencies": {
    "@babel/runtime": "^7.14.0",
    "@react-native-community/async-storage": "^1.12.1",
    "@react-native-community/cli-platform-ios": "^4.13.0",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "^6.0.0",
    "@react-navigation/bottom-tabs": "^5.11.2",
    "@react-navigation/material-top-tabs": "^5.3.10",
    "@react-navigation/native": "^5.8.10",
    "@react-navigation/stack": "^5.12.8",
    "@sentry/react-native": "^2.3.0",
    "aws-sdk": "^2.897.0",
    "babel-preset-react-app": "^7.0.0",
    "base64-arraybuffer": "^0.2.0",
    "jest-cli": "^26.6.3",
    "json-groupby": "^1.1.0",
    "moment": "^2.29.1",
    "query-string": "^7.0.0",
    "react": "16.13.1",
    "react-dom": "^16.13.1",
    "react-native": "^0.64.0",
    "react-native-barcode-mask": "^1.2.4",
    "react-native-base64": "^0.2.1",
    "react-native-camera": "^3.43.3",
    "react-native-checkbox-lite": "^1.0.2",
    "react-native-countdown-component": "^2.7.1",
    "react-native-datepicker": "^1.7.2",
    "react-native-device-info": "^8.1.3",
    "react-native-elements": "^3.3.1",
    "react-native-firebase": "^5.6.0",
    "react-native-flatlist-slider": "^1.0.5",
    "react-native-fs": "^2.17.0",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-material-tabs": "^4.1.2",
    "react-native-mime-types": "^2.3.0",
    "react-native-modal-dropdown": "^1.0.0",
    "react-native-modal-overlay": "^1.3.1",
    "react-native-passmeter": "0.0.5",
    "react-native-phone-number-input": "^2.0.0",
    "react-native-qrcode-svg": "^6.0.6",
    "react-native-reanimated": "^2.1.0",
    "react-native-safe-area-context": "^3.1.9",
    "react-native-screens": "^2.16.1",
    "react-native-segmented-control-tab": "^3.4.1",
    "react-native-sha256": "^1.3.6",
    "react-native-snap-carousel": "^3.9.1",
    "react-native-sqlite-storage": "^5.0.0",
    "react-native-svg": "^12.1.0",
    "react-native-tab-view": "^2.16.0",
    "react-native-ui-xg": "0.0.6",
    "react-native-vector-icons": "^7.1.0",
    "react-native-webview": "^11.4.0",
    "react-navigation": "^4.4.2",
    "react-navigation-drawer": "^2.5.2",
    "react-navigation-stack": "^2.8.4",
    "react-redux": "^7.2.3",
    "react-scripts": "^4.0.3",
    "redux": "^4.0.5",
    "redux-thunk": "^2.3.0",
    "url": "^0.11.0"
  },
  "devDependencies": {
    "@babel/core": "7.11.6",
    "@babel/preset-env": "^7.14.1",
    "@babel/runtime": "^7.14.0",
    "@react-native-community/eslint-config": "1.1.0",
    "babel-jest": "^26.6.3",
    "eslint": "6.8.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "0.59.0",
    "react-test-renderer": "^16.14.0"
  },
  "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native)",
      "node_modules/?!(react-navigation)"
    ]
  }
}

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset',"@babel/react", "@babel/env"],
};

应用-test.js

import 'react-native';
import React from 'react';
import renderer from 'react-test-renderer';
import App from '../src/App';

it('renders correctly', () => {
  renderer.create(<App />);
});

我在 StackOverflow 和互联网上尝试了许多公认的答案,但没有运气解决这个问题。这些是我记得的一些链接,可能有点不相关,因为除了 @react-navigation 之外,我在其他模块上遇到了同样的问题:

https://stackoverflow.com/a/59802557/14792325

https://stackoverflow.com/a/54463740/14792325

https://stackoverflow.com/a/55803188/14792325

https://stackoverflow.com/a/43197503/14792325

https://lifesaver.codes/answer/react-native-syntaxerror-unexpected-token-import

谁能帮我找出我的错误?非常感谢。

标签: javascriptreact-nativejestjsreact-navigationbabel-jest

解决方案


推荐阅读