Ionic build/run throws "Error: Unexpected token in JSON at position 0"

#1

Just using the simple ‘build’ command but getting JSON error, but it does not provide a name for which file! All I get are the printout between the dashed lines:


ionic build android

ionic-hello-world@ ionic:build C:\workspace2016\xxx\ionic\xxx-xxx
ionic-app-scripts build

[11:09:28] ionic-app-scripts 0.0.47
[11:09:28] build dev started …
[11:09:28] clean started …
[11:09:28] clean finished in less than 1 ms
[11:09:28] copy started …
[11:09:28] transpile started …
[11:09:32] transpile finished in 4.37 s
[11:09:32] webpack started …
[11:09:33] copy finished in 4.75 s
[11:09:42] webpack finished in 9.54 s
[11:09:42] sass started …
[11:09:44] sass finished in 1.64 s
[11:09:44] build dev finished in 15.60 s

Error: Unexpected token in JSON at position 0


Checked the usual files: package.json, tsconfig.json etc… but not spotting any issues.

Is there a way to get build tool to be a bit more friendly? Relatively new to ionic development. Thanks.

#2

Have you tried running ionic serve instead and seeing what errors are thrown up in the typescript compiler/browser console?

#3

Unfortunately, ‘ionic serve’ runs fine with no issues - hence I’m more baffled!

#5

Unfortunately without seeing what custom code you may have added trying to diagnose the issue is pure guesswork

#6

Usually this relates to a problem with either:

  • JSON response from a server call not being returned (or is returned malformed)
  • Problem with the url for a http request being malformed/incorrect

Are you making a http call in your app code and trying to parse data expected to be returned from that?

#7

Thanks saintsatplay - posting this question is a last desperate attempt as I’m out of ideas and have spent the last two days googling with no success.

The code base is quite big for this app and it would be impossible to back track everything I’ve done. The application was developing fine until a couple of days ago as I noticed my changes were not getting compiled into new versions. Since then I’ve uninstall and reinstalled pretty much everything (cordova, ionic, node etc…) finally got ‘ionic serve’ to show new changes in code but this one is a killer as it does not show which JSON file is the problem!

I’m guessing a standard module JSON file being corrupted somehow by my installations but impossible to know which one when there are thousands of them??!!

#8

In Ionic serve can you use the Browser inspector tools to analyse the network requests and see what responses are being returned for http requests? This might help narrow down if there is JSON not able to be returned properly (I.e. a malformed response) or not at all - which is what might be causing your app to choke on build process.

Alternatively, If you’re using a CVS like Git or BitBucket for example you could try rolling back to different versions and seeing whether certain files have made a difference.

Other than that all I can recommend is creating a new app and re-creating each file (pasting the contents from the old into the new) and testing one at a time (OR up to the last point where you know it was working and then diagnose which amendments afterwards cause the issue).

I get that you’ve got a large codebase to work with - and it ain’t easy - but those would be my best suggestions for what they’re worth.

#9

Can you do the following

npm ls typescript

And also list the version of app-scripts in package.json

#10

C:\xxx\xxx\xxx\xxx-xxx>npm ls typescript
ionic-hello-world@0.0.1 C:\xxx\xxx\xxx\xxx-xxx
`-- typescript@2.1.4

package.json content:

{
“name”: “ionic-hello-world”,
“author”: “Ionic Framework”,
“version”: “0.0.1”,
“homepage”: “http://ionicframework.com/”,
“private”: true,
“scripts”: {
“ionic:build”: “ionic-app-scripts build”,
“ionic:serve”: “ionic-app-scripts serve”
},
“dependencies”: {
"@angular/common": “2.1.1”,
"@angular/compiler": “2.1.1”,
"@angular/compiler-cli": “2.1.1”,
"@angular/core": “2.1.1”,
"@angular/forms": “2.1.1”,
"@angular/http": “2.1.1”,
"@angular/platform-browser": “2.1.1”,
"@angular/platform-browser-dynamic": “2.1.1”,
"@angular/platform-server": “2.1.1”,
"@ionic/storage": “1.1.6”,
“assign”: “^0.1.7”,
“ionic-angular”: “2.0.0-rc.3”,
“ionic-native”: “2.2.3”,
“ionicons”: “3.0.0”,
“lodash”: “^4.17.2”,
“lodash.assign”: “^4.2.0”,
“rxjs”: “5.0.0-beta.12”,
“uuid”: “^3.0.1”,
“zone.js”: “0.6.26”
},
“devDependencies”: {
"@ionic/app-scripts": “0.0.47”,
“typescript”: “2.1.4”
},
“description”: “trading-buddy: An Ionic project”,
“cordovaPlugins”: [
“cordova-plugin-device”,
“cordova-plugin-console”,
“cordova-plugin-whitelist”,
“cordova-plugin-splashscreen”,
“cordova-plugin-statusbar”,
“ionic-plugin-keyboard”
],
“cordovaPlatforms”: []
}

#11

I thought TS 2.1 wasn’t usable for Ionic yet. Does the problem persist with 2.0.9?

#12

unfortunately, yes:

C:\xxx\xxx\xxx\xxx-xxx>npm ls typescript
ionic-hello-world@0.0.1 C:\xxx\xxx\xxx\xxx-xxx
`-- typescript@2.0.9

C:\xxx\xxx\xxx\xxx-xxx>ionic build android

ionic-hello-world@0.0.1 ionic:build C:\xxx\xxx\xxx\xxx-xxx
ionic-app-scripts build

[22:58:44] ionic-app-scripts 0.0.47
[22:58:44] build dev started …
[22:58:44] clean started …
[22:58:44] clean finished in 15 ms
[22:58:44] copy started …
[22:58:44] transpile started …
[22:59:03] transpile finished in 19.52 s
[22:59:04] webpack started …
[22:59:04] copy finished in 20.45 s
[22:59:20] webpack finished in 16.68 s
[22:59:20] sass started …
[22:59:24] sass finished in 3.52 s
[22:59:24] build dev finished in 39.80 s

Error: Unexpected token in JSON at position 0

#13

ionic-app-scripts 0.0.48 was released several hours ago, might be worth a try.

#14

Thanks rapropos, unfortunately still same ERROR for app-scripts 0.0.48 with TS 2.0.9 & 2.1.4.

#15

Having the same problem. It appeared after computer restarted while running ionic run command. I managed to get ionic serve working again by deleting C:\Users\Diego.ionic\ionic.config file which was corrupted. However until now I am not able to get ionic run or ionic build working again

#16

Bump, however my issue is a little different

Error: Unexpected token / in JSON at position 404
#17

Have you resolved Error: Unexpected token in JSON at position 0 issue while build?
I’m facing same problem.
If you have any solution please reply.
Thanks.

1 Like
#18

I resolve this problem by removing platform (i.e. ionic platform rm android). And add platform (i.e. ionic platform add android) again.

#19

You probably printed something on the server side like stdclass … just return json
the print_r() in this example Lead to json error

    $json = file_get_contents('php://input');
    $obj = json_decode($json);

    print_r($obj);// this line <-

	$user = new User();
	$user->scenario = User::SCENARIO_CREATE;
    $value = (array) $obj;
	$user->attributes = $value;

    
	if($user->validate()){
		$user->save();
		return array('status' => true, 'data' => "user created successfully");
	} else {
		return array('status' => false, 'data' => $user->getErrors());
	}
#20

FWIW I saw this error when there was a git merge conflict and the /plugins/ios.json file still had an unmerged <<<<<<HEAD line line in it

1 Like
#21

That worked for me to !!!