Skip to content

pg-protocol: issue with imports in 1.9.0 with webpack #3434

Closed
@colatkinson

Description

@colatkinson

When bundling code with 1.9.0, we're hitting issues related to the new ESM wrapper.

During build, webpack prints a bunch of these warnings:

WARNING in ../node_modules/pg-protocol/esm/index.js 11:15-23
export 'default' (imported as 'protocol') was not found in '../dist/index.js' (possible exports: DatabaseError, __esModule, parse, serialize)

At runtime, the code fails to execute with the following error:

webpack://redacted/node_modules/pg-protocol/esm/index.js:5
export const DatabaseError = protocol.DatabaseError
                             ^


TypeError: Cannot read properties of undefined (reading 'DatabaseError')
    at Object.11078 (webpack://redacted/node_modules/pg-protocol/esm/index.js:5:30)
    ...

Looking at the way imports are done in the ESM wrapper:

import protocol from '../dist/index.js'

This seems to assume a default export from dist/index.js. Copying the generated code below:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseError = exports.serialize = exports.parse = void 0;
const messages_1 = require("./messages");
Object.defineProperty(exports, "DatabaseError", { enumerable: true, get: function () { return messages_1.DatabaseError; } });
const serializer_1 = require("./serializer");
Object.defineProperty(exports, "serialize", { enumerable: true, get: function () { return serializer_1.serialize; } });
const parser_1 = require("./parser");
function parse(stream, callback) {
    const parser = new parser_1.Parser();
    stream.on('data', (buffer) => parser.parse(buffer, callback));
    return new Promise((resolve) => stream.on('end', () => resolve()));
}
exports.parse = parse;
//# sourceMappingURL=index.js.map

I believe that node (at least as of 20.19) works around this with a "synthetic" default export. However webpack, because the code sets exports.__esModule = true, treats this as an ESM module and thus doesn't shim a default where there is none.

To resolve this, both of these approaches seem to work in some quick testing:

  1. Change the import to import * as protocol from '../dist/index.js' . I believe this is correct with regards to how ESM imports are supposed to work.
  2. Change tsconfig to generate code that isn't marked as an ES module (i.e. doesn't generate that __esModule stub). Removing this line by hand seems to work, though I have no idea which set of levers to pull to effect this change in the actual codegen.

Happy to contribute a PR towards a fix if either of these or an alternative approach sounds feasible.

Alternatively, if this is simply too far outside supported use cases, feel free to close. It's possible to work around by adding a

{
    resolve: {
        alias: {
            "pg-protocol": path.resolve(__dirname, "node_modules/pg-protocol/dist/index.js"),
        },
    },
}

to the webpack.config.js.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions