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

问题描述 投票:0回答:1

我正在上 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 中使用命令行创建文件

javascript node.js reactjs createfile
1个回答
0
投票

如果你想创建一个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}`);
});
© www.soinside.com 2019 - 2024. All rights reserved.