evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.14k stars 1.15k forks source link

Empty esmodules in dependencies cause `ReferenceError: exports2 is not defined` when building for node #910

Closed airhorns closed 3 years ago

airhorns commented 3 years ago

I am trying to bundle a module from npm (graphql-ws) that includes a file in it's tarball (node_modules/graphql-ws/lib/types.mjs) with these contents:


"use strict";
/**
 *
 * types
 *
 */
Object.defineProperty(exports, "__esModule", { value: true });

When I use esbuild to bundle a module that depends on this package, esbuild compiles it to:

// node_modules/graphql-ws/lib/types.mjs
var require_types = __commonJS(() => {
  __markAsModule(exports2);
});

which causes a reference error at runtime when trying to require the built module:

ReferenceError: exports2 is not defined
    at /Users/airhorns/Code/esbuild-test/build/test.js:622:18
    at /Users/airhorns/Code/esbuild-test/build/test.js:11:5
    at /Users/airhorns/Code/esbuild-test/build/test.js:648:17
    at /Users/airhorns/Code/esbuild-test/build/test.js:11:5
    at Object.<anonymous> (/Users/airhorns/Code/esbuild-test/build/test.js:7780:34)
    at Module._compile (node:internal/modules/cjs/loader:1091:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1120:10)
    at Module.load (node:internal/modules/cjs/loader:971:32)
    at Function.Module._load (node:internal/modules/cjs/loader:812:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)

because the exports2 variable that's normally passed in to the module closure is not.

I am bundling with esbuild --bundle --target=es2017 --platform=node on v0.8.54. Here's a full reproduction where you can run yarn test to demonstrate the issue: https://github.com/airhorns/esbuild-empty-module-repro

The built code is just fine when not using --platform=node as well.

Thanks for any help you can give me!

evanw commented 3 years ago

Thanks for the report. This problem should be fixed in the next release (not released yet).