Skip to content

Browser-only runtime error on path.resolve in defaultBundler #1816

@blackening

Description

@blackening

Reproduction

const res = await testInContext("1+1;", {
  chapter: Chapter.SOURCE_2
});

res.context.errors[0]
// -> TypeError: Cannot read properties of undefined (reading 'resolve')
//    at defaultBundler (bundler.ts:46:39)
//    at preprocessFileImports (index.ts:100:21)
//    at async sourceFilesRunner (index.ts:115:28)
//    at async runFilesInContext (index.ts:239:35)
//    at async testInContext (workspace-context.ts:86:20)
// (the last line is my code, but copied from js-slang as mentioned above)

Diagnosis

The error originates from bundler.ts:

path.resolve(...)

In the browser, path is not defined, so path.resolve throws. The error ends up inside the context.

Solution

Polyfill path.

For Vite projects:

npm install path-browserify

In vite.config.ts:

export default defineConfig({
  plugins: [...],
  resolve: {
    alias: {
      // point `path` imports to the browser shim
      'path': 'path-browserify'
    }
  }
});

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions