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 | ### Use-case example
|
7 |
|
8 | ```js
|
9 | $ echo '{ "name": "test", some garbage' > package.json
|
10 | $ npm install whatever
|
11 | npm ERR! install Couldn't read dependencies
|
12 | npm ERR! Failed to parse json
|
13 | npm ERR! Unexpected token s
|
14 | npm ERR! File: /tmp/package.json
|
15 | npm ERR! Failed to parse package.json data.
|
16 | npm ERR! package.json must be actual JSON, not just JavaScript.
|
17 | ```
|
18 |
|
19 | This isn't helpful.
|
20 |
|
21 | ```js
|
22 | $ echo '{ "name": "test", some garbage' > package.json
|
23 | $ yapm install whatever
|
24 | error - Failed to parse json
|
25 | Unexpected token 's' at 1:19
|
26 | { "name": "test", some garbage
|
27 | ^
|
28 | error - File: /tmp/package.json
|
29 | error - Failed to parse package.json data.
|
30 | package.json must be actual JSON, not just JavaScript.
|
31 | ```
|
32 |
|
33 | 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.
|
34 |
|
35 | 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.
|
36 |
|
37 | ### Discussions
|
38 |
|
39 | 1. "have npm report the error with a package.json" - [github issue](https://github.com/npm/npm/issues/3869)
|
40 | 2. "make JSON.parse return error positions" - [github PR](https://github.com/npm/npm/pull/4373)
|
41 |
|