如何在 webpack 中使用 child_process [英] How to use child_process in webpack

查看:84
本文介绍了如何在 webpack 中使用 child_process的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在托管 Web 应用程序时在目录中运行命令行函数,但是当我尝试运行以下代码时,它抛出无法解析模块‘child_process’":

I'm trying to run command line functions in a directory while hosting a web application, but when I try to run the following code, it throws "Cannot resolve module 'child_process'":

var exec = require('child_process').exec;

它与 webpack 一起运行.

It's running with webpack.

推荐答案

它与 webpack 一起运行.

It's running with webpack.

你的意思是它与 webpack 捆绑在一起了吗?

Do you mean its been bundled with webpack?

无论哪种方式,如果您尝试捆绑 child_process 模块以供客户端使用,您都无法做到.它需要 Node Core 才能运行,因为它会在 os 级别生成/分叉一个新进程.它不会自动生成 WebWorker 或某些浏览器等价物.

Either way, if you're trying to bundle the child_process module for use client-side, you won't be able to. It needs the Node Core in order to run since it spawns/forks a new process at the os level. Its not going to automagically spawn a WebWorker or some browser equivalent.

以上适用于其他 Node Core API,它们只是不可移植,因为它们存在,它们旨在在服务器端运行.这个 github repo 有一个 Node.js 核心 API 的列表,这些 API 已经被移植到浏览器.

The above is true for the other Node Core API's, they just aren't portable as they exist, they're meant to be run server-side. This github repo has a list of Node.js Core API's that have been ported to the browser.

这篇关于如何在 webpack 中使用 child_process的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆