Skip to content

ReferenceError: exports is not defined in ES module scope #773

@aminya

Description

@aminya

Describe the bug

Vitest vscode extension gives this error when it loads the tests in a workspace. I have set up all my package types to module, so it should not error about this.
‍‍‍‍‍‍‍‍‍ReferenceError: exports is not defined in ES module scope

Expected behavior

It should not error for exports usage. I am not using commonjs

Reproduction

Open Vitest in a bun workspace and add configurations via vitest.config.ts for one of the packages.

import solid from "vite-plugin-solid";
import { defineConfig } from "vitest/config";

export default defineConfig({
	plugins: [solid()],
	resolve: {
		conditions: ["development", "browser"],
	},
	test: {
		environment: "happy-dom",
		globals: true,
		include: ["src/**/__tests__/**/*.test.{ts,tsx}"],
	},
});

My vite version is 7.3.1

Here's package.json:

{
	"name": "test-workspace",
	"private": true,
	"main": "src/index.ts",
	"type": "module",
	"dependencies": {
		"solid-js": "^1.9.11"
	},
	"devDependencies": {
		"@solidjs/testing-library": "^0.8.10",
		"@testing-library/user-event": "^14.6.1",
		"@testing-library/dom": "^10.4.1",
		"@testing-library/jest-dom": "^6.9.1",
		"happy-dom": "^20.7.0",
		"vite": "^7.3.1",
		"vite-plugin-solid": "^2.11.10",
		"vitest": "^4.0.18"
	}
}

and tsconfig (merged from extends for easier understanding

{
	"extends": "./node_modules/@tsconfig/bases/strictest.tsconfig.json",
	"compilerOptions": {
		"strictBuiltinIteratorReturn": false,
		"strictPropertyInitialization": false,
		"forceConsistentCasingInFileNames": true,
		"target": "es2022",
		"module": "esnext",
		"moduleResolution": "bundler",
		"lib": ["es2022"],
		"noEmit": true,
		"preserveWatchOutput": true,
		"allowImportingTsExtensions": true,
		"esModuleInterop": true,
		"preserveSymlinks": true,
		"declaration": true,
		"sourceMap": true,
		"incremental": true,
		"allowJs": true,
		"esModuleInterop": true,
		"jsx": "preserve",
		"jsxImportSource": "solid-js",
		"lib": ["ES2022", "DOM", "DOM.Iterable"],
		"types": ["vite/client"],
		"moduleResolution": "bundler",
		"skipLibCheck": true,
		"sourceMap": true,
		"strict": true
	},
	"exclude": ["node_modules", "dist"]
}

Output

Error: Vitest failed to start: 
ReferenceError: exports is not defined in ES module scope
    at file:///Users/amin/work/test/packages/auth/node_modules/.vite-temp/vitest.config.js.timestamp-1775796412003-477f52e87ef0f.mjs:9:23
    at ModuleJob.run (node:internal/modules/esm/module_job:345:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:665:26)
    at async loadConfigFromBundledFile (file:///Users/amin/work/test/node_modules/.bun/vite@7.3.1+31d0b22d5eb96c53/node_modules/vite/dist/node/chunks/config.js:35909:12)
    at async bundleAndLoadConfigFile (file:///Users/amin/work/test/node_modules/.bun/vite@7.3.1+31d0b22d5eb96c53/node_modules/vite/dist/node/chunks/config.js:35797:17)
    at async loadConfigFromFile (file:///Users/amin/work/test/node_modules/.bun/vite@7.3.1+31d0b22d5eb96c53/node_modules/vite/dist/node/chunks/config.js:35764:42)
    at async resolveConfig (file:///Users/amin/work/test/node_modules/.bun/vite@7.3.1+31d0b22d5eb96c53/node_modules/vite/dist/node/chunks/config.js:35413:22)
    at async _createServer (file:///Users/amin/work/test/node_modules/.bun/vite@7.3.1+31d0b22d5eb96c53/node_modules/vite/dist/node/chunks/config.js:25362:67)
    at async createViteServer (file:///Users/amin/work/test/node_modules/.bun/vitest@4.1.2+b66a349cc098673f/node_modules/vitest/dist/chunks/cli-api.Bxr1Nn49.js:8820:17)
    at async Module.createVitest (file:///Users/amin/work/test/node_modules/.bun/vitest@4.1.2+b66a349cc098673f/node_modules/vitest/dist/chunks/cli-api.Bxr1Nn49.js:14161:18)
	at e.u (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/extension.js:1:58873)
	at e.emit (node:events:519:28)
	at e.o.t.exports.ae (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:38577)
	at e.o.t.exports.emit (node:events:519:28)
	at e.o.t.exports.dataMessage (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:16501)
	at e.o.t.exports.getData (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:15398)
	at e.o.t.exports.startLoop (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:12110)
	at e.o.t.exports._write (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:11402)
	at writeOrBuffer (node:internal/streams/writable:572:12)
	at _write (node:internal/streams/writable:501:10)
	at Writable.write (node:internal/streams/writable:510:10)
	at Socket.Z (/Users/amin/.vscode/extensions/vitest.explorer-1.50.1/dist/wrapper-Br94zcvg.js:1:39601)
	at Socket.emit (node:events:519:28)
	at addChunk (node:internal/streams/readable:561:12)
	at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
	at Readable.push (node:internal/streams/readable:392:5)
	at TCP.onStreamRead (node:internal/stream_base_commons:189:23)
	at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

Extension Version

1.50.1

Vitest Version

4.1.2

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions