1 | Display presize location of errors in JSON documents
|
2 | ----------------------------------------------------
|
3 |
|
4 | Sometimes 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
|
13 | npm ERR! install Couldn't read dependencies
|
14 | npm ERR! Failed to parse json
|
15 | npm ERR! Unexpected token s
|
16 | npm ERR! File: /tmp/package.json
|
17 | npm ERR! Failed to parse package.json data.
|
18 | npm ERR! package.json must be actual JSON, not just JavaScript.
|
19 | ```
|
20 |
|
21 | This 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 |
|
35 | This 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 |
|
37 | Same 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 |
|
41 | 1. "have npm report the error with a package.json" - [github issue](https://github.com/npm/npm/issues/3869)
|
42 | 2. "make JSON.parse return error positions" - [github PR](https://github.com/npm/npm/pull/4373)
|
43 |
|