输出已安装 node.js 库的所有许可证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16274841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output all licenses of installed node.js libraries
提问by whadar
Is there an option in npm (or other tool) to print all used licenses? I have a project and I want to make sure I don't use a library which is under a license I can't use.
npm(或其他工具)中是否有打印所有使用过的许可证的选项?我有一个项目,我想确保我不使用我无法使用的许可证下的库。
EDIT: Found out that many developers don't include the license in the package.json, so I had to find out manually using "npm docs package-name"
编辑:发现许多开发人员没有在 package.json 中包含许可证,所以我不得不使用“npm docs package-name”手动找出
采纳答案by Andreas Hultgren
cd {project}/node_modules
ls | sed 's/$/\/package.json/' | xargs grep '"license[s]*"' -A 3
Could use some improvement, but it works (at least on osx, should work on linux, no idea about windows). You should see something like:
可以使用一些改进,但它有效(至少在 osx 上,应该在 linux 上工作,不知道 Windows)。你应该看到类似的东西:
grunt/package.json: "licenses": [
grunt/package.json- {
grunt/package.json- "type": "MIT",
grunt/package.json- "url": "http://github.com/gruntjs/grunt/blob/master/LICENSE-MIT"
--
grunt-contrib-concat/package.json: "licenses": [
grunt-contrib-concat/package.json- {
grunt-contrib-concat/package.json- "type": "MIT",
grunt-contrib-concat/package.json- "url": "https://github.com/gruntjs/grunt-contrib-concat/blob/master/LICENSE-MIT"
--
Update:
更新:
If you wish to see the name of all modules, even those nested inside other modules, the following works (cred to @robertklep, slightly modified to still work when inside the node_modules directory):
如果您希望查看所有模块的名称,即使是那些嵌套在其他模块中的模块,以下工作(信任@robertklep,稍作修改以在 node_modules 目录中仍然有效):
find * -name package.json | xargs grep '"license[s]*"' -A 3
回答by iandotkelly
I had exactly the same requirement, and wrote a node module to do this. Shameless self promotion I know, but it is open source and hope it can help resolve your issue. Let me know if you have any issues or suggestions.
我有完全相同的要求,并编写了一个节点模块来做到这一点。无耻的自我宣传我知道,但它是开源的,希望它可以帮助解决您的问题。如果您有任何问题或建议,请告诉我。
The difference over the other answers is that it does not just use the package.json license declaration, but looks for potential license information in license and readme files in the project.
与其他答案的不同之处在于它不只是使用 package.json 许可证声明,而是在项目的许可证和自述文件中查找潜在的许可证信息。
You can install using npm install -g nlf
您可以使用安装 npm install -g nlf
回答by jstaab
Yarn has a command for this as well. yarn licenses listrenders short output, yarn licenses generate-disclaimerrenders all the actual license textto stdout (suitable for disclaimers, as the option would imply).
Yarn 也有这方面的命令。yarn licenses list呈现短输出,yarn licenses generate-disclaimer将所有实际许可文本呈现到标准输出(适用于免责声明,正如该选项所暗示的那样)。
If you want to omit
devDependencies:NODE_ENV=production yarn licenses listFor my purposes, the following command got me close enough:
yarn licenses list | grep License | \ grep -vE 'MIT|ISC|WTFPL|BSD|Apache|Unlicense|CC-BY|Public Domain'`
如果你想省略
devDependencies:NODE_ENV=production yarn licenses list出于我的目的,以下命令使我足够接近:
yarn licenses list | grep License | \ grep -vE 'MIT|ISC|WTFPL|BSD|Apache|Unlicense|CC-BY|Public Domain'`
回答by Yaniv Kessler
Take a look at license-reportor license-checker
回答by jlevy
Having just done this for a large project, I can say it turns out this process is more of a headache to automate fully than you might think. It's easy to get many of them with some of the tricks listed here, but NPM package licenses are not published consistently, and can appear
刚刚为一个大型项目完成了这项工作,我可以说这个过程比你想象的要完全自动化更令人头疼。使用这里列出的一些技巧很容易获得其中的许多,但是 NPM 包许可证并没有一致地发布,并且可能会出现
- In the NPM package.json file, or
- In the README file (sometimes just the name, like "MIT license", and sometimes full license text in a section), or
- In a separate LICENSE or COPYING file.
- 在 NPM package.json 文件中,或
- 在 README 文件中(有时只是名称,如“MIT 许可证”,有时是一个部分中的完整许可证文本),或
- 在单独的 LICENSE 或 COPYING 文件中。
In addition, you sometimes have to read a licenses to tell which well-known open source license it corresponds to.
此外,有时您必须阅读许可证才能知道它对应于哪个著名的开源许可证。
The best tool I know to do this, that (unlike some of the other answers here) covers all these cases is the licensecheckpackage: https://github.com/marcello3d/node-licensecheck
我知道这样做的最佳工具(与此处的其他一些答案不同)涵盖所有这些情况的是licensecheck包:https: //github.com/marcello3d/node-licensecheck
It looks at package.json as well as common license files, and does a signature match against known licenses, so it accurately recognizes more licenses automatically. It also "normalizes" licenses against the standard SPDX list of licenses (https://spdx.org/licenses/).
它查看 package.json 以及常见的许可证文件,并与已知许可证进行签名匹配,因此它可以自动准确识别更多许可证。它还根据标准 SPDX 许可证列表 ( https://spdx.org/licenses/)对许可证进行“规范化” 。
Finally, Licensecheck also lets you save any remaining packages you needed to manually verify in your own license.json file (since you can't count on an external maintainer to change their package).
最后,Licensecheck 还允许您将需要手动验证的任何剩余包保存在您自己的 license.json 文件中(因为您不能指望外部维护者来更改他们的包)。
Taken together, this is a pretty robust solution.
总之,这是一个非常强大的解决方案。
回答by EaterOfCode
I liked the question, and took the time to write a nodejs script for it:
我喜欢这个问题,并花时间为它编写了一个 nodejs 脚本:
var npm = require('npm');
npm.load(process.config,function(err){
npm.list(function(err,deps){
var names = Object.keys(deps.dependencies);
for(var i in names){
var depen = deps.dependencies[names[i]];
console.log('Licenses for :',names[i]);
depen.licenses.forEach(function(license,i){
console.log('License #'+(i+1));
console.log('- Title:',license.type);
console.log('- Url:\t',license.url);
});
}
});
});
this will output each license name and url for each module,
这将输出每个模块的每个许可证名称和 URL,
NOTE:must be executed in project folder and npm must be installed (npm install npm -gsounds overkill but this is the npm js lib)
注意:必须在项目文件夹中执行,并且必须安装 npm(npm install npm -g听起来有点矫枉过正,但这是 npm js lib)
回答by dmx
You can try this it you are on a Linux based system:
您可以在基于 Linux 的系统上尝试此操作:
npm list -g --depth=0 | awk '{print }' | xargs -i npm view {} | grep license
You will have something like this:
你会有这样的事情:
license: 'MIT',
license: 'MIT',
license: 'MIT',
license: 'BSD',
license: 'MIT',
license: 'MIT',
license: 'BSD-2-Clause',
license: 'MIT',
.....................
.....................
.....................
license: 'BSD-2-Clause',
回答by Brett Zamir
If you're using Atom, there is npm-license-checkerto get the licenses from package.json.
如果您使用 Atom,可以使用npm-license-checker从package.json.

