如何快速检查是否运行npm install Node

下一个脚本考虑所有文件夹并安装依赖项

var fs = require( "fs" ), path = require( "path" ), child_process = require( "child_process" ); var rootPath = "./"; var dirs = fs.readdirSync( rootPath ) .filter( function( dir ) { return fs.statSync( path.join( rootPath, dir )).isDirectory(); }); var install = function() { if ( dirs.length === 0 ) return; var dir = dirs.shift(); console.log( "installing dependencies for : '" + dir + "'" ); child_process.exec( "npm prune --production | npm install", { cwd: rootPath + dir }, install ); }; install(); 

只有在文件夹中存在package.json的情况下如何运行npm install命令?

试试这个命令:

 ls | grep package.json && (npm prune --production | npm install) 

我假设你在Linux中运行这个。

理论上,如果我没有记错的话,命令ls的输出将被传送给grep命令,只有当grep命令find结果时,才会执行命令(npm prune --production | npm install)

在写这篇文章的时候,这并没有经过我的testing,因为我现在没有一个Linux机器来testing它,但是我希望它能正常工作。

更新:有效的命令,根据丹的评论将是

 test -f package.json && (npm prune --production | npm install)