Skip to content

webpack output is not treated as a module (import.meta) #20088

@arikorn

Description

@arikorn

Bug Description

Goal: get import.meta to be "ignored" correctly by webpack.

Some context: in Bitfocus Companion, with its current webpack settings,
import.meta.url (in @sentry/node-core/build/esm/sdk/esmLoader.js) is converted to a hardcoded string containing the full dev environment path -- since the goal is distribute the code, this is an error, and since the "problem" is in a third-party package, I can't simply change their code. Also note that the code works correctly before packaging.

Setting module.parser.javascript.importMeta: false in webpackconfig fixes that problem in the code (i.e. import.meta is left in the code) but it causes other errors including a variation on this simplified one described below.)

No matter what I have tried, so far, one thing or another breaks... Here's one example:

Link to Minimal Reproduction and step to reproduce

Please see code at: https://github.com/arikorn/wptest

To run (assuming corepack is enabled?, etc.):

yarn
# test that the code works:
node FixImports.mts  

# now pack and test the packed code:
npx webpack
node dist/main.mjs

Expected Behavior

both unpacked and packaged code should run to completion.

Actual Behavior

Unpacked code is fine but with packed code, running it results in the following error:

$ node dist/main.mjs
<anonymous_script>:20
        const testPath = path__WEBPACK_IMPORTED_MODULE_1___default().join(import.meta.dirname, 'node_modules/bufferutil')
                                                                                 ^^^^

SyntaxError: Cannot use 'import.meta' outside a module

Note that the "entrypoint" filename, the package.json, and the packaged filename all indicate that it's a module.

I have tried numerous variations in the config file while keeping module.parser.javascript.importMeta: false but with no effect. In the bitfocus repo I've also tried things like turning importMeta off and setting module.parser.javascript.url: 'relative' but that both causes other errors and does not substitute a relative URL for import.meta.url (i.e. it's still a full path)

Incidentally, in the Bitfocus repo version of this, if I substitute __dirname for import.meta.dirname node complains that __dirname is not defined in the packaged code! I.e. the code errors whether you assume it's a module or not! (In this simplified version, as long as node.__dirname is set to true, this code-fix works. Though of course it's technically wrong to do so, and breaks the unpackaged version.

Environment

System:
    OS: Linux 6.6 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (6) x64 Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
    Memory: 5.08 GB / 7.68 GB
  Binaries:
    Node: 22.21.0 - /run/user/1000/fnm_multishells/2456183_1762289908966/bin/node
    Yarn: 4.10.3 - /mnt/c/Program Files/nodejs/yarn
    npm: 10.9.4 - /run/user/1000/fnm_multishells/2456183_1762289908966/bin/npm
  Packages:
    webpack: ^5.102.1 => 5.102.1 
    webpack-cli: ^6.0.1 => 6.0.1

Is this a regression?

None

Last Working Version

No response

Additional Context

As mentioned above, this is a very simplified version of what I've been encountering with Bitfocus Companion. If you're able and willing to evaluate that more complex setup too -- i.e., if fixing this doesn't help? -- I could provide instructions. That repo is much more finicky, as mentioned above, and even setting type: "module" in dist/package.json causes the packaged code to fail ("require is not defined" somewhere). Just to be clear, the same setting in the base dev folder works just fine, it's only when applied to the packaged code folder that it causes a problem.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions