ParcelJS does not bundle imported js library
Asked Answered
P

1

9

I have a simple NodeJS app (A Cloudflare Worker).

In the main typescript file (index.ts) I import the handler module which has this as the first line: const Monitor = import('ping-monitor')

ping-monitor is a dependency in package.json:

"dependencies": {
    "ping-monitor": "^0.6.2"
  }

Now when I run parcel build src/index.ts the output in the dist has: var $1ZQrD$pingmonitor = require("ping-monitor");

So the contents of ping-monitor are not bundled properly. And when I try to run this locally via wrangler dev (output is dist/worker.js) it complains: Uncaught ReferenceError: require is not defined.

How can I tell ParcelJS to bundle ping-monitor inside the output?

Phantasy answered 29/11, 2021 at 22:34 Comment(2)
what syntax is this? const Monitor = import('ping-monitor').... Shouldn't it be const Monitor = require('ping-monitor')?Semivowel
I'm not sure. The linter was complaining about require so had to use import therePhantasy
B
12

By default, parcel won't bundle any dependencies for nodejs build targets. If you want to bundle all dependencies you can use the includeNodeModules option (see docs), like this:

{
   ...
   "main": "dist/worker.js",
   "targets": {
      "main": {
         "includeNodeModules": true
      }
   }
}

(You can also specify "whitelist" of modules to include).

Beaumarchais answered 29/11, 2021 at 23:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.