你如何设置Node.js的terminal标签标题?

我知道这可以从terminal手动完成使用:

echo -n -e "\033]0;My terminal tab title\007"

我试图把它放到console.logprocess.stdout.write然后摆弄出来,但是我无法让它工作。

为了节省读者的时间,下面是一个在严格模式下执行的function:

 function setTerminalTitle(title) { process.stdout.write( String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7) ); } 

有一个节点库: node-bash-title

在节点中的使用

要安装库:

 npm install node-bash-title --save 

在您的脚本中:

 const setTitle = require('node-bash-title'); setTitle('🍻 Server'); 

用于NPM脚本

这个包还提供了一个可执行的脚本。 你可以在你的npm脚本中使用它。 例如:

 "scripts": { "start:dev": "set-bash-title server && node server/app.js" "start:prod": "node server/app.js" }, 

所以你可以从代码中删除标题脚本。 此外,如果您想设置标题(开发模式),则只能设置标题。 在生产模式下,您可能不想设置标题,因为您的脚本可能不会在XTerm中执行:)