UNPKG

1.63 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### Use-case example
7
8```js
9$ echo '{ "name": "test", some garbage' > package.json
10$ npm install whatever
11npm ERR! install Couldn't read dependencies
12npm ERR! Failed to parse json
13npm ERR! Unexpected token s
14npm ERR! File: /tmp/package.json
15npm ERR! Failed to parse package.json data.
16npm ERR! package.json must be actual JSON, not just JavaScript.
17```
18
19This 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
33This 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
35Same 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
391. "have npm report the error with a package.json" - [github issue](https://github.com/npm/npm/issues/3869)
402. "make JSON.parse return error positions" - [github PR](https://github.com/npm/npm/pull/4373)
41