Parcel: Error: Process is not defined (in web worker)

Created on 13 Jul 2018  路  5Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

Starting up a web worker in electron seems to yield

worker.4ea33c72.js:1 Uncaught ReferenceError: process is not defined
(anonymous) @ worker.4ea33c72.js:1

I see another closed issue with this error but it seems to be related to electron and web workers in the browser half of the environment.

馃帥 Configuration (.babelrc, package.json, cli command)

.tsconfig

{
  "compilerOptions": {
    "sourceMap": true,
    "lib": [ "es2017", "dom" ],
    "downlevelIteration": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": [
    "index.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

Also, providing absolute paths is the only way plugins will work, because relative ones don't work.

{
  "plugins": [
    "C:\\Users\\jtenner\\Desktop\\projects\\project-revoluti0n\\babel-plugins\\script-transformer.js",
  ]
}

馃 Expected Behavior

I guess if process isn't defined, define it. I don't know where this code is generated, but it's literally the first line of my code for the worker, and I can't get it running.

馃槸 Current Behavior

image

It's the first line of the generated worker.

馃拋 Possible Solution

No idea.

馃捇 Code Sample

The code is going to be MIT so I don't mind sharing the entry point.

import config from "../../application.config";
import { IPointClickEvent } from "../events/IPointClickEvent";
import { IPointMoveEvent } from "../events/IPointMoveEvent";
import { IWorkerEvent } from "../events/IWorkerEvent";
import { IInteractionPoint } from "../util";
import { ISprite } from "../view/Sprite";
import { IStageManagerProps, StageManager } from "./StageManager";

const props: IStageManagerProps = {
  audioContext: new AudioContext(),
  height: config.window.height,
  selector: "body",
  width: config.window.width,
};

const sm = new StageManager(props);
const worker = new Worker("../worker/index.js");

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.9.4
| Node | 10.6.0
| npm/Yarn | 1.7.0
| Operating System | Windows
| electron | 2.0.4
| electron-builder | 20.19.2

I have no problem trying to help fix this in any way. Thanks for the help!

Question

Most helpful comment

Solved the problem.

In your main.js file of your electron environment, you must enable the nodeIntegrationInWorker parameter like this:

  mainWindow = new BrowserWindow({
    width: config.window.width,
    height: config.window.height,
    webPreferences: {
      nodeIntegrationInWorker: true
    },
  });

I'm now staring down the barrel of a different problem/error which is great news!

I have the following recommendations/suggestions:

  1. Can we please inform electron developers that they need to enable this option?
  2. Can parcel not make the assumptions it makes about electron inside a webworker?

All 5 comments

I don't think the entrypoint is the issue, as it'll be MIT. Could you share the entire codebase? Ot at least the part with the bug?

Solved the problem.

In your main.js file of your electron environment, you must enable the nodeIntegrationInWorker parameter like this:

  mainWindow = new BrowserWindow({
    width: config.window.width,
    height: config.window.height,
    webPreferences: {
      nodeIntegrationInWorker: true
    },
  });

I'm now staring down the barrel of a different problem/error which is great news!

I have the following recommendations/suggestions:

  1. Can we please inform electron developers that they need to enable this option?
  2. Can parcel not make the assumptions it makes about electron inside a webworker?

This is out of scope of parcel however you could request electron to show a warning for that

Sent with GitHawk

Okay. If parcel doesn't make assumptions about the environment, can we at least put a line in our documentation where the --target=electron options appears?

Example:

For electron users who want to use workers, please enable the webPreferences: { nodeIntegrationInWorker: true } property when creating your browser window.

It would have saved me at least three hours of headaches.

Feel free to open up a pr in parcel/website repo

Sent with GitHawk

Was this page helpful?
0 / 5 - 0 ratings