Skip to content

@jupyter-widgets/controls: require() call breaks ES module #3854

Open
@thomasjm

Description

@thomasjm

Description

There is a stray call to require in what is otherwise a perfectly good ES module here:

export const version = (require('../package.json') as any).version;

When this is published to NPM, it is transpiled from TypeScript but not bundled. So the production source looks the same: an ES module with a random require at the bottom. See here in lib/index.js.

If I try to bundle this package using Rollup into a library targeting ESM, and then use it in a web browser, that require remains there and causes a crash, because require doesn't exist in browsers. I'm doing this via Vite, which uses Rollup to compile for production.

There may be some advanced options for Rollup which will cause it to properly transform the require in a "mixed ES module" like this, which could serve as a workaround. But I think it would be more sensible to just remove the require. It seems its only purpose is to import the version string from package.json. I bet there's a way to insert it directly during the build process instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions