简体   繁体   English

如何使用 zx 在 package.json 中运行脚本?

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

I use zx to create a script .我使用 zx 创建一个脚本。 and I run yarn web:build in my zx.mjs script.我在 zx.mjs 脚本中运行yarn web:build but it return errors:但它返回错误:

Error: Cannot find module 'worker_threads'

why and how to fix it?为什么以及如何解决它?

It may be because it was removed by something that zx uses.可能是因为它被zx使用的东西删除了。 Try installing the yarn/node package again, for node it's npm install worker_threads, or yarn, which is yarn add worker_threads.再次尝试安装yarn/node 包,对于node,它是npm install worker_threads,或者yarn,即yarn add worker_threads。 It may also be that zx might reject the package, so try using it normally, not in your zx script.也有可能是 zx 拒绝了这个包,所以尝试正常使用它,而不是在你的 zx 脚本中。

I use node api(exec) to save this question.我使用 node api(exec) 来保存这个问题。 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:并在 zx 脚本中导入此脚本:

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

it works.有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM