Skip to content

vmThreads CommonJS this isn't bound to module.exports #11501

Closed

Description

@lstanden

Describe the bug

We hit this loading FlexSearch in vmThreads and narrowed it down to how the VM loader calls CommonJS modules. Top-level this isn't bound to module.exports, as it is in Node. In a strict-mode module, it's undefined.

Reproduction

reproduction.zip

The reproduction uses Vitest 5.0.3, a local CommonJS dependency, and no custom config.

The dependency's index.cjs is:

'use strict';
this.value = 42;
exports.isExports = this === module.exports;

The test:

import { expect, test } from 'vitest';
import commonjs from 'commonjs-this-fixture';

test('binds CommonJS top-level this to exports', () => {
  expect(commonjs).toEqual({ value: 42, isExports: true });
});

This passes in Node and with --pool=threads. With either --pool=vmThreads or --pool=vmForks, it fails during import:

TypeError: Cannot set properties of undefined (setting 'value')

I'd expect it to pass in all three pools.

CommonjsExecutor._compile calls the wrapper without a receiver. Binding it to this.exports makes the test pass in both VM pools:

-fn(this.exports, this.require, this, filename, __dirname)
+fn.call(this.exports, this.exports, this.require, this, filename, __dirname)

The patch was tested against the published 5.0.3 package. The linked main revision has the same call, but hasn't been tested with this patch.

I used Codex to help isolate the problem and test the patch.

System Info

System:
  OS: macOS 26.6.2
  CPU: (18) arm64 Apple M5 Max
  Memory: 443.28 MB / 64.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 22.15.0 - /Users/lee.standen/.volta/tools/image/node/22.15.0/bin/node
  Yarn: 1.22.19 - /Users/lee.standen/.volta/tools/image/yarn/1.22.19/bin/yarn
  npm: 10.9.2 - /Users/lee.standen/.volta/tools/image/node/22.15.0/bin/npm
  pnpm: 11.24.0 - /Users/lee.standen/.volta/bin/pnpm
Browsers:
  Chrome: 154.0.8037.93
  Safari: 26.6.2
npmPackages:
  vitest: 5.0.3 => 5.0.3

Used Package Manager

pnpm

Validations

Activity

  1. added theissue type on Oct 5, 2026
  2. locked and limited conversation to collaborators on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions