Worker crashing because it is unable to parse `./node_modules/@napi-rs/snappy-darwin-arm64/snappy.darwin-arm64.node`

I’m currently trying to spawn a worker in my jest test like so:

  Runtime.install({
      logger: new DefaultLogger('WARN', (entry: LogEntry) =>
        console.log(`[${entry.level}]`, entry.message)
      )
    });
    testEnv = await TestWorkflowEnvironment.createLocal();
    nativeConnection = testEnv.nativeConnection;
    temporalClient = testEnv.client;

    const worker = await Worker.create(
      workflowCoverage.augmentWorkerOptions({
        connection: nativeConnection,
        taskQueue: 'test',
        workflowsPath: require.resolve('../../../src/web/temporal/workflow'),
        activities
      })
    );

The issue is that the worker crashes as is it unable to parse ./node_modules/@napi-rs/snappy-darwin-arm64/snappy.darwin-arm64.node here is the relevant stack trace:

      at DefaultLogger.worker_1.Runtime.install.logger [as logFunction] (tests/workflows/e2e/denseReconstruction.test.ts:81:17)

  console.log
    [ERROR]

      at DefaultLogger.worker_1.Runtime.install.logger [as logFunction] (tests/workflows/e2e/denseReconstruction.test.ts:81:17)

  console.log
    [ERROR] ERROR in ./node_modules/@napi-rs/snappy-darwin-arm64/snappy.darwin-arm64.node 1:0

      at DefaultLogger.worker_1.Runtime.install.logger [as logFunction] (tests/workflows/e2e/denseReconstruction.test.ts:81:17)

  console.log
    [ERROR] Module parse failed: Unexpected character '�' (1:0)

      at DefaultLogger.worker_1.Runtime.install.logger [as logFunction] (tests/workflows/e2e/denseReconstruction.test.ts:81:17)

  console.log
    [ERROR] You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

I know that I can configure my worker to be able to parse binary files, but from what I understand this should be already configured by the Typescript SDK so I’m wondering if I’m missing anything?

Here is my yarn.lock file:

The error message you are seeing comes from the Workflow Bundler, which internally use Webpack. The bundler take your Workflow Code (i.e. src/web/temporal/workflow.ts in your case) as well as all dependencies recursively imported from that file, and produce a single JS file which can be loaded into the sandboxed workflow execution context.

Now, it seems like your workflow code is somehow trying to import snappy. According to your yarn.lock file, that’s probably a transitive import resulting from a direct import on winston-loki from your workflow code. Does that make sense?

In all cases, you are not allowed to import non-deterministic-safe modules from your workflow code. Log exporters and native modules are both inherently unsafe in that regard, and are therefore not supported inside the workflow context.

Solution

If your intention is to funnel your workflow logs through Winston, simply register your log exporter on Runtime.logger; then, in your workflow code, use the log object provided by the SDK itself. See this sample for details. Make sure that your workflow code no longer import winston.

If that is not appropriate for your use case, please provide some more context.