简体   繁体   English

如何使用 node.js 的控制台日志打开命令提示符

[英]How to open a command prompt with the console log from node.js

I have node.js and I want to open a cmd file with the console log when I run "node namefile.js".我有 node.js 并且我想在运行“node namefile.js”时使用控制台日志打开一个 cmd 文件。 So when I run the js file with "node namefile.js" it opens a cmd with the message I wrote example: console.log("Hello World;");因此,当我使用“node namefile.js”运行 js 文件时,它会打开一个 cmd,其中包含我编写的消息示例:console.log("Hello World;"); It will open a cmd with "Hello World!"它将打开一个带有“Hello World!”的 cmd

The js file needs to create the cmd or open a new cmd as i know.据我所知,js文件需要创建cmd或打开一个新的cmd。 I don't want to make a.cmd or.bat file and script the js file to open it.我不想制作 .cmd 或 .bat 文件并编写 js 文件脚本来打开它。

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

exec('start cmd.exe @cmd /k "echo Hello World!', (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    if(stdout)
        console.log(`stdout: ${stdout}`);
});

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

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