在指定文件夹中以编程方式运行npm install

时间:2018-11-14 18:06:25

标签: node.js npm

我想通过打字脚本代码在指定目录中运行npm install

我找到了以下代码:

npm.load({}, function(err: any) {
    // handle errors

    // install module ffi
    npm.commands.install(["hello-world@0.0.1"], function(err: any, data: any) {
    // log errors or data
    });

    npm.on('log', function(message: any) {
    // log installation progress
    console.log(message);
    });
});

但是现在我不想安装hello-world,而只需运行npm install(不带任何软件包)。

此外,它应该在我可以指定的路径中运行,例如./folder/subfolder

我该怎么做?

3 个答案:

答案 0 :(得分:1)

除了exec以外,还可以使用npm软件包:

import * as cp from 'child_process';
var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';

var path = '/path_to_npm_install';
const result = cp.spawnSync( npm, ['install'], {
     cwd: path
}); 

答案 1 :(得分:0)

如果您使用的是Nodejs(我认为是这样),则可以运行

child_process.exec('npm install') // or any other command which you give from terminal or command prompt

检查child_process的文档 https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

答案 2 :(得分:0)

您可以创建一个期望来自用户的目录路径的nodejs List<Y> ListOfY = new List<Y>(); IEnumerable<X> EnumerableOfX = ListOfY; // No issue ,并创建一个子进程并在其中执行该命令。

index.js

script

从nodejs文档中了解有关const { exec } = require('child_process'); exec(`cd /${process.env.PATH} | npm install`, (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`); }); PATH=/path_of_directory_to_run_npm_install node index.js 的更多信息-https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback