{"record":{"id":"602b0324bad5dabf","repo":"denoland/deno","slug":"err-invalid-arg-type-602b03","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"options.env\" property must be of type object or one of undefined, null, or worker_threads.SHARE_ENV. Received ${envOpt}","messagePattern":"The \"options\\.env\" property must be of type object or one of undefined, null, or worker_threads\\.SHARE_ENV\\. Received (.+?)","errorType":"validation","errorClass":"ERR_INVALID_ARG_TYPE","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/worker_threads.ts","lineNumber":421,"sourceCode":"        }\n      }\n    }\n\n    // Serialize workerData before resolving the filename so that\n    // DataCloneError is thrown before file-not-found errors,\n    // matching Node.js behavior.\n\n    // Handle the `env` option following Node.js semantics:\n    // - undefined/null: snapshot current lazyProcess().default.env (isolated copy)\n    // - SHARE_ENV: worker shares the parent's OS environment\n    // - object: use that object, coercing values to strings\n    // - anything else: throw ERR_INVALID_ARG_TYPE\n    // See https://github.com/denoland/deno/issues/23522.\n    let env_ = undefined;\n    const envOpt = options?.env;\n    if (envOpt != null && envOpt !== SHARE_ENV) {\n      if (typeof envOpt !== \"object\") {\n        throw new ERR_INVALID_ARG_TYPE(\n          \"options.env\",\n          [\"object\", \"undefined\", \"null\", \"worker_threads.SHARE_ENV\"],\n          envOpt,\n        );\n      }\n      // Snapshot the provided env, coercing values to strings like Node.js.\n      // This also handles passing `lazyProcess().default.env` (a Proxy in Deno) by\n      // producing a plain object that can be structured-cloned.\n      const envObj = {};\n      const keys = ObjectKeys(envOpt);\n      for (let i = 0; i < keys.length; i++) {\n        envObj[keys[i]] = String(envOpt[keys[i]]);\n      }\n      env_ = envObj;\n    } else if (envOpt !== SHARE_ENV) {\n      // Default: snapshot current lazyProcess().default.env so the worker gets an\n      // isolated copy, not a live reference to the OS environment.\n      // Wrap in try/catch because accessing lazyProcess().default.env requires","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/worker_threads.ts#L403-L439","documentation":"Worker's env option follows Node semantics: undefined/null snapshots the parent env, worker_threads.SHARE_ENV shares the live OS environment, and an object is copied with String()-coerced values into a structured-clone-safe snapshot. Any other type — string, number, boolean — fails the typeof check and throws ERR_INVALID_ARG_TYPE('options.env', ...) with the accepted-type list embedded in the message.","triggerScenarios":"new Worker(f, { env: 'production' }), { env: 123 }, or { env: true } — a non-object, non-nullish value that is not SHARE_ENV.","commonSituations":"Confusing env (a key-to-value map) with a NODE_ENV-style name; passing a JSON.stringify()'d env received from CI variables or message queues; copy-paste from CLI docs where env values are strings.","solutions":["Pass a plain object: { env: { NODE_ENV: 'production', DEBUG: 'app:*' } }.","To share the parent's live environment, pass worker_threads.SHARE_ENV.","To inherit a snapshot, simply omit env (or pass null).","If env arrives serialized, parse it first: { env: JSON.parse(envJson) }."],"exampleFix":"// before\nconst w = new Worker(f, { env: 'production' });\n\n// after\nimport { SHARE_ENV, Worker } from 'node:worker_threads';\nconst w = new Worker(f, { env: { NODE_ENV: 'production' } });\n// or share the parent's live env:\nconst w2 = new Worker(f, { env: SHARE_ENV });","handlingStrategy":"type-guard","validationCode":"import { SHARE_ENV } from 'node:worker_threads';\n\nif (env !== undefined && env !== null && env !== SHARE_ENV && typeof env !== 'object') {\n  throw new TypeError('options.env must be an object, undefined, null, or SHARE_ENV');\n}\nconst w = new Worker(f, { env });","typeGuard":"function isValidWorkerEnv(v: unknown): v is Record<string, string> | undefined | null {\n  return v == null || v === SHARE_ENV || typeof v === 'object';\n}","tryCatchPattern":null,"preventionTips":["Type the option explicitly: env?: Record<string, string> | typeof SHARE_ENV | null.","Never pass serialized env strings; parse JSON at the boundary.","Add a unit test asserting the env option shape before spawning pools."],"tags":["worker-threads","env","arguments","node-compat"],"backgroundTag":"invalid-argument-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}