我有一个简单的包裹。json文件,我想添加一个评论。有办法做到这一点吗,或者有什么hack可以做到这一点吗?
{
"name": "My Project",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongoose": "3.x"
},
"devDependencies" : {
"should": "*"
/* "mocha": "*" not needed as should be globally installed */
}
}
上面的示例注释在npm崩溃时不起作用。我还尝试了// style注释。
NPS(节点包脚本)为我解决了这个问题。它允许你把NPM脚本放到一个单独的JavaScript文件中,在那里你可以添加大量的注释和任何其他你需要的JavaScript逻辑。
https://www.npmjs.com/package/nps
来自我的一个项目的package-scripts.js示例
module.exports = {
scripts: {
// makes sure e2e webdrivers are up to date
postinstall: 'nps webdriver-update',
// run the webpack dev server and open it in browser on port 7000
server: 'webpack-dev-server --inline --progress --port 7000 --open',
// start webpack dev server with full reload on each change
default: 'nps server',
// start webpack dev server with hot module replacement
hmr: 'nps server -- --hot',
// generates icon font via a gulp task
iconFont: 'gulp default --gulpfile src/deps/build-scripts/gulp-icon-font.js',
// No longer used
// copyFonts: 'copyfiles -f src/app/glb/font/webfonts/**/* dist/1-0-0/font'
}
}
我只是做了一个本地安装npm安装nps -save-dev,并把它放在我的包。json脚本。
"scripts": {
"start": "nps",
"test": "nps test"
}
As of pnpm 7.17.1, which was just released, you can switch to pnpm for package management, move your package.json to package.json5, and comments in package.json5 are allowed and will be preserved by pnpm. Note however that for publishing as a package to use on the npm registry (for example), a package.json5 will not be recognized by other package managers and I doubt by all of the registry's processing. So you would have to convert the package.json5 to a package.json before publishing. On the other hand, for "top-level applications" that are unlikely to be included as packages in other projects, a package.json5 seems to work just fine, as long as you then stick with pnpm as your package manager.
最近在Node.js邮件列表中讨论了这个问题。
根据创建npm的Isaac Schlueter的说法:
... "//"键永远不会被NPM用于任何目的,它是为注释保留的…如果想使用多行注释,可以使用数组或多个“//”键。
当使用你常用的工具(npm, yarn等)时,多个“//”键将被移除。这生存:
{ "//": [
"first line",
"second line" ] }
这将不复存在:
{ "//": "this is the first line of a comment",
"//": "this is the second line of the comment" }
必须注意,"//"只能在包的根目录下使用。json对象。例如
{
“/ /”:“评论!”
“依赖”:{…}
}
是有效的但是
{
“依赖”:{
“/ /”:“评论?”
}
}
是无效的。
——@david_p评论
总结一下这些答案:
添加一个名为//的顶级字段,其中包含一个注释字符串。这是可行的,但它很糟糕,因为你不能把评论放在他们正在评论的东西附近。
添加多个以//开头的顶级字段,例如包含注释字符串的//dependencies。这样更好,但仍然只允许您做顶级注释。不能注释单个依赖项。
在脚本中添加echo命令。这是可行的,但它很糟糕,因为您只能在脚本中使用它。
这些解决方案的可读性也都不是很好。它们增加了大量的视觉干扰,ide不会用语法将它们作为注释突出显示。
我认为唯一合理的解决方案是生成包。Json从另一个文件。最简单的方法是将JSON写成JavaScript,然后使用Node.js将其写入package.json。将该文件保存为package.json。chmod +x它,然后你可以运行它来生成你的package。json。
#!/usr/bin/env node
import { writeFileSync } from "fs";
const config = {
// TODO: Think of better name.
name: "foo",
dependencies: {
// Bar 2.0 does not work due to bug 12345.
bar: "^1.2.0",
},
// Look at these beautify comments. Perfectly syntax highlighted, you
// can put them anywhere and there no risk of some tool removing them.
};
writeFileSync("package.json", JSON.stringify({
"//": "This file is \x40generated from package.json.mjs; do not edit.",
...config
}, null, 2));
它使用//键来警告人们不要编辑它。\x40生成是故意的。它变成了@generated in package。Json,这意味着一些代码审查系统将在默认情况下崩溃该文件。
这是构建系统中的一个额外步骤,但它胜过这里的所有其他hack。