在 Node 中使用 touch 命令创建新的空文件

编程入门 行业动态 更新时间:2024-10-27 06:26:38

在 Node 中使用 touch <a href=https://www.elefans.com/category/jswz/34/1771323.html style=命令创建新的空文件"/>

在 Node 中使用 touch 命令创建新的空文件

我正在上 Node.js 的在线课程..它说要使用“touch”命令创建新的空 JS 文件 -

touch script.js
但是我收到了这个错误 -

$ node
Welcome to Node.js v18.14.2.
Type ".help" for more information.

> touch dummy2.js
touch dummy2.js
      ^^^^^^

Uncaught SyntaxError: Unexpected identifier
>

我是否应该使用不同的命令在命令行中创建一个新文件,因为我正在使用 Windows? 我通读了这个但是对我没有用- 在 Node 中使用命令行创建文件

回答如下:

如果你想创建一个shell进程并在其中执行命令,你可以使用exec()函数。 试试这个

const { exec } = require('child_process');

exec('touch dummy2.js', (error, stdout, stderr) => {
  if (error) {
    console.error(`error: ${error.message}`);
    return;
  }

  if (stderr) {
    console.error(`stderr: ${stderr}`);
    return;
  }

  console.log(`stdout:\n${stdout}`);
});

更多推荐

在 Node 中使用 touch 命令创建新的空文件

本文发布于:2024-05-13 15:22:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   文件   Node   touch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!