radiorz
radiorz

Reputation: 1879

How to run script in the package.json with zx?

I use zx to create a script . and I run yarn web:build in my zx.mjs script. but it return errors:

Error: Cannot find module 'worker_threads'

why and how to fix it?

Upvotes: 0

Views: 980

Answers (2)

Josiah
Josiah

Reputation: 33

It may be because it was removed by something that zx uses. Try installing the yarn/node package again, for node it's npm install worker_threads, or yarn, which is yarn add worker_threads. It may also be that zx might reject the package, so try using it normally, not in your zx script.

Upvotes: 0

radiorz
radiorz

Reputation: 1879

I use node api(exec) to save this question. But I think it should be more directly and easy to use:

import { exec } from "child_process";
export default function runScript(script = "") {
  exec(script, (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`);
      return;
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
  });
}

and import this script in zx script:

import runScript from './runScript.mjs'
runScript(`yarn run web:build`);

it works.

Upvotes: 1

Related Questions