ronsap123
ronsap123

Reputation: 183

Very odd Cordova/Phonegap JSON error: Unexpected token in JSON at position 0

So i've been developing an app for the past few weeks. I have never messed with any json file whatsoever. Today I tried to run my app and it shows a very odd error from Cordova, Error:

Error: Unexpected token   in JSON at position 0

The token is ' '? Which JSON file is it? I went over all the main Json package files, and all of them started with the 0'th token as a '{'.

I'm completely lost, I didn't do anything at all, I didn't add a plugin or something that day it just appeared out of no where. I have no idea which json files to attach here, there are dozens of them in the project directory.. Any ideas or direction will be extremely helpful, thanks.

Upvotes: 6

Views: 4247

Answers (4)

Osasere
Osasere

Reputation: 36

My approach to any JSON error:

  1. [optional] Remove/uninstall all plugins

  2. Delete all your platform directory folders (ie. browser, Android, iOS etc,), and delete the package.json file

    Note: Don't delete the package-lock.json file

  3. Create a new package.json file by running npm init on your command line (run this command on your Cordova project directory).

    Make sure you create this file with the same package name you used before. If you don't remember, you can check the config.xml file (including the 'version').

  4. Reinstall all your plugins (if you followed step 1).

  5. Add your platforms.

  6. Run, build your Cordova project.

    • If it fails, follow step 1 to the last step.

    • It works 100℅

    • If this doesn't work, contact me and send me your issues.

Upvotes: 0

Rajath Kedilaya
Rajath Kedilaya

Reputation: 751

Run npm install and it will give you more details of the error. In my case, it was an extra comma in package.json. It is a painful bug and I wish cordova explicitly mentioned where this issue was!

Upvotes: 2

Jacksonkr
Jacksonkr

Reputation: 32207

Check plugins/*.json

There's most likely an issue hiding in one of those files.

For me it was an unresolved merge conflict inside fetch.json

Upvotes: 4

OussamaChaib
OussamaChaib

Reputation: 37

You had to check the file fetch.json in Plugins folder. Fetch.json must be encode in UTF8 and at the end of the JSON, the file need to finish correctly, without a comma, like this :

{
  "es6-promise-plugin": {
    "source": {
      "type": "registry",
      "id": "es6-promise-plugin@^4.1.0"
    },
    "is_top_level": false,
    "variables": {}
  },
  "cordova-plugin-nativestorage": {
    "source": {
      "type": "registry",
      "id": "cordova-plugin-nativestorage@^2.3.2"
    },
    "is_top_level": true,
    "variables": {}
  } //No Comma
}

Good luck !

Upvotes: 0

Related Questions