node.js - 通过前端安装 NPM 模块
问题描述
我正在开发一个应用程序,我希望能够通过前端安装 NPM 模块。不过,我不知道该怎么做。也就是说,我知道如何通过前端进行 CRUD 操作,但我不知道如何与命令行交互或通过前端运行命令行功能。
有没有可以帮助解决这个问题的包,或者它是否以某种方式内置于 Node.js 中?
简而言之,如何以可以安装 NPM 包的方式将前端连接到后端?
解决方案
你想要的是child_process模块。它是内置的,因此您无需安装任何额外的模块。
大多数情况下,您要查找的是spawn()
or 或exec()
。
例如,如果您想运行npm install some_module
,您可以执行以下操作:
const { exec } = require('child_process');
let command = 'npm install some_module';
let options = { cwd: '/path/to/node/project' };
exec(command, options, (error, stdout, stderr) => {
// Do anything you want with program output here:
console.log('output:', stdout, stderr);
});
推荐阅读
- javascript - 调用带有 Axios 的 JS 函数,调用时返回未定义
- apache-storm - 为什么 acked 的数字可能与 Storm Spout 上发出的数字有很大差异?
- json - 如何使用 kotlin 以可重用的方式使用关键属性比较 2 个 Json 对象文件?
- javascript - 如何访问浏览器音频流(来自浏览器的声音)。有这方面的API吗?
- android - 在 android studio 3.6.3 中创建新项目时出现问题
- node.js - 如何使用 NodeJS 在 MongoDB 中执行文本搜索查询?
- c - 为什么这个 Makefile 会无限次执行程序?
- r - 为什么升级到 R 4.0.0 后 xts.index 停止工作?
- node.js - 使用 Node 和 Express 在按钮单击时渲染 pug 页面
- python - 如何为函数参数“*argv”添加解析器参数