Skip to content

threepointone/workers-process-access-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

workers-process-access-error

This is a repro for a bug with the cloudflare vite plugin

The issue: we use unenv to polyfill node.js compatibility in workers. However, with the vite plugin, globalThis.process gets assigned with the polyfilled process object only after importing and evaluating dependencies, which means any dependency that uses process will not have access to the polyfilled process object.

For this discussion, I made a module called early-process-access that logs the value of process.stderr, the contents of which are simply:

console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

And I have a worker, that imports this module, and then also logs the same itself, like so:

import "early-process-access";

console.log("in main worker, logging process.stderr", process.stderr);

export default {
  async fetch(request, env, ctx) {
    return new Response("Hello, world!");
  },
};

When we run dev/prod, we expect this log:

inside early-process-access, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }
in main worker, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }

wrangler (works as expected)

when using wrangler, this works as expected. Indeed, if you run wrangler build, and inspect the output, you'll see code in this order:

// ...
// node_modules/wrangler/_virtual_unenv_global_polyfill-@cloudflare-unenv-preset-node-process
globalThis.process = process_default;

// node_modules/early-process-access/index.js
console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

// index.js
console.log("in main worker, logging process.stderr", process.stderr);
// ...

vite (does not work as expected)

when using vite, the output is:

inside early-process-access, logging process.stderr undefined

in main worker, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }

we see undefined there, because process.stderr is not polyfilled yet. Indeed, if you run vite build and inspect the output, you'll see code in this order:

//...
console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

globalThis.process = _process;

console.log("in main worker, logging process.stderr", process.stderr);
// ...

(note that globalThis.process gets assigned after the early-process-access module is imported and evaled.)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors