UNPKG

1.71 kBMarkdownView Raw
1Display presize location of errors in JSON documents
2----------------------------------------------------
3
4Sometimes you have an invalid `package.json`. It might be a simple typo, it might be a filesystem issue, or something else.
5
6![screenshot](https://raw.github.com/rlidwka/yapm/master/changes/images/json-err.png)
7
8### Use-case example
9
10```js
11$ echo '{ "name": "test", some garbage' > package.json
12$ npm install whatever
13npm ERR! install Couldn't read dependencies
14npm ERR! Failed to parse json
15npm ERR! Unexpected token s
16npm ERR! File: /tmp/package.json
17npm ERR! Failed to parse package.json data.
18npm ERR! package.json must be actual JSON, not just JavaScript.
19```
20
21This isn't helpful.
22
23```js
24$ echo '{ "name": "test", some garbage' > package.json
25$ yapm install whatever
26 error - Failed to parse json
27 Unexpected token 's' at 1:19
28 { "name": "test", some garbage
29 ^
30 error - File: /tmp/package.json
31 error - Failed to parse package.json data.
32 package.json must be actual JSON, not just JavaScript.
33```
34
35This is, because you see that it is not JSON, and you see an exact position of the error, it happened in line 1 column 19.
36
37Same thing happens whenever `JSON.parse()` is called. So if you receiving non-json data from npm registry, you'll also clearly see where the error is. Look at the [issue #4449](https://github.com/npm/npm/issues/4449). Can you spot where that big ugly JSON fails to parse? With this patch it will be easy.
38
39### Discussions
40
411. "have npm report the error with a package.json" - [github issue](https://github.com/npm/npm/issues/3869)
422. "make JSON.parse return error positions" - [github PR](https://github.com/npm/npm/pull/4373)
43