javascriptwebpackpostmark

Using module "child_process" without Webpack


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?


Solution

  • 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.