【问题标题】:React native : Unexpected token in JSON at position 0反应本机:位置0的JSON中的意外标记
【发布时间】:2019-06-07 16:50:11
【问题描述】:

我是原生反应的新手。我想用反应导航做一个项目。我收到了这个错误:

SyntaxError: Unexpected token   in JSON at position 0
at JSON.parse (<anonymous>)
at FileStore.get(:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\stores\FileStore.js:26:19)
at C:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\Cache.js:76:40
at Generator.next (<anonymous>)
at step (C:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\Cache.js:18:30)
at C:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\Cache.js:37:14
at new Promise (<anonymous>)
at C:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\Cache.js:15:12
at Cache.get (C:\Users\fz\Documents\androidproject\test2\node_modules\metro-cache\src\Cache.js:102:7)
at C:\Users\fz\Documents\androidproject\test2\node_modules\metro\src\DeltaBundler\Transformer.js:166:34

这是我的 app.js:

import React from 'react';
import Input from './input.js';
import Kalkulator from './kalkulator.js';
import { createBottomTabNavigator, createAppContainer } from 'react-        
        navigation'; // Version can be specified in package.json

const RootStack = createBottomTabNavigator({
    Input: Input,
    Kalkulator: Kalkulator
  }
);

export default createAppContainer(RootStack);

这是我的 package.json:

{
  "name": "test2",
  "version": "0.0.1",
  "private": true,
  "scripts": {
  "start": "node node_modules/react-native/local-cli/cli.js start",        
  "test": "jest",
  "android-linux": "react-native bundle --platform android --dev false --      
    entry-file index.js --bundle-output 
    android/app/src/main/assets/index.android.bundle --assets-dest 
    android/app/src/main/res && react-native run-android"
  },
  "dependencies": {
      "react": "16.6.3",
      "react-native": "0.57.8",
      "react-native-gesture-handler": "^1.0.12",
      "react-navigation": "^3.0.9"
   },
   "devDependencies": {
      "babel-jest": "23.6.0",
      "jest": "23.6.0",
      "metro-react-native-babel-preset": "0.51.1",
      "react-test-renderer": "16.6.3"
    },
    "jest": {
      "preset": "react-native"
    }
  }

我试图在 google 上搜索,但我找不到解决此问题的方法,在我构建这个项目之前,我曾尝试在我之前的项目中使用 react navigation 并且很好,但是为什么现在我得到这个错误?

【问题讨论】:

  • 该问题不包含相关代码。见stackoverflow.com/help/mcveFileStore 不在任何地方使用。该错误意味着存在无效的 JSON。
  • 嗨,可能是因为模块中的一些错误,试试这个:1:删除文件夹'node_modules' 2:在项目目录的终端中运行npm install
  • @amirhosein 我已经尝试过了,但我仍然遇到这样的错误
  • @estus 对不起,如果我的问题不包含相关代码,我很困惑,因为我没有以 json 格式传递任何数据,如果我删除 import react-navigation 没有错误,但是当我import react-navigation 会出现这样的错误
  • 我明白了。这可能意味着缓存文件由于某种原因而损坏。这就是堆栈跟踪所说的。您可以调试所述 FileStore.get 并检查它所指的文件。只需修改 node_modules 中的源文件并将 console.log 放置在这里和那里以了解 JSON 文件路径,然后导航到那里并检查它有什么问题。这是 react-native 本身的问题,请考虑在 repo 中打开一个问题(您需要以任何方式检查该 JSON 有什么问题)。

标签: reactjs react-native npm react-navigation


【解决方案1】:

我今天遇到了同样的问题。我花了几个小时寻找解决方案。没有任何效果。 最后,我做了这样的事情:

尝试运行此命令-

npm start -- --reset-cache

在您的项目根目录中,此错误将得到解决。

这个命令的目的是重置缓存后启动npm。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2020-10-31
    • 2021-10-22
    • 2021-05-21
    • 2021-12-21
    相关资源
    最近更新 更多