Using module “child_process” without Webpack

心不动则不痛 提交于 2019-12-10 17:35:04

问题


I'm using Webpack to bundle dependencies, one of which is the emailing service postmark. This service depends upon something called child_process that apparently ships with node.

The problem is, when I attempt to run webpack to bundle my app, it complains:

Module not found: Error: Cannot resolve module 'child_process' in ...

Most of the answers online say that, in response to this error, I should add the line:

  node: {
    child_process: 'empty'
  }

to my webpack config. But this makes no sense, because then webpack just doesn't try to look for child_process, and consequently, when I run my app, I get the following error:

Uncaught TypeError: exec is not a function

which is an error from postmark (the service that relies upon child_process) complaining that the exec function within the child_process module doesn't exist.

Thus, I'm wondering how I can include the child_process module in my build without webpack complaining?


回答1:


Front-end developer of Postmark here.

It looks like you're trying to bundle postmark.js into your client-side JavaScript but https://github.com/wildbit/postmark.js is a Node.js library. This means it will not run in the browser and requires a running Node.js server.

The main reason for not supporting browser environment is security. As you can see in this example:

var postmark = require("postmark");
var client = new postmark.Client("<server key>");

it requires you to insert the auth token. Bundling this with your client-side JS would expose it and allow everyone access your postmark account API.

Hope this clarifies it.



来源:https://stackoverflow.com/questions/34983446/using-module-child-process-without-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!