发布NPM包遇JSON解析错误Unexpected token },求解决方案
解决NPM包上传时的JSON解析错误
嘿,我刚看到你在上传NPM包时碰到的这个JSON解析错误,这绝对是新手容易踩的坑——你的package.json里存在**尾随逗号(trailing commas)**的问题,JSON语法严格禁止这种写法(虽然JavaScript对象字面量允许),这就是导致解析失败的根源。
你的package.json里的错误点:
author对象中,最后一个键值对"license": "ISC",后面多了逗号bugs对象中,"email": "8b21espq@gmail.com",后面多了逗号dependencies里的"Math": "",后面多了逗号(另外提一句:Math是JavaScript内置对象,不需要作为依赖安装,这个条目可以直接删掉)keywords数组的最后一项"package",后面多了逗号
修正后的完整package.json:
{ "name": "M-P-Formulas-JS", "version": "1.0.0", "description": "The M-P-Formulas package for JavaScript. This package allows the user to use math and physics formulas such as the Pythagorean Theorem.", "main": "mpformulasJS.js", "author": { "email": "8b21espq@gmail.com", "name": "Jeff Lockhart", "url": "", "license": "ISC" }, "bugs": { "email": "8b21espq@gmail.com" }, "dependencies": {}, "keywords": [ "mpformulas", "mpformulasjs", "m-p-formulas-js", "math", "physics", "formulas", "module", "package" ] }
额外建议:
你可以用NPM自带的命令快速验证package.json的语法正确性:
npm pkg validate
如果返回valid就说明没问题了,要是还有错误也会给出具体的位置提示,非常实用。
内容的提问来源于stack exchange,提问作者Jeff Lockhart
相关产品推荐
相关产品推荐

