gpt4 book ai didi

javascript - Node.js 读取行 : Unexpected token =>

转载 作者:IT老高 更新时间:2023-10-28 23:23:29 29 4
gpt4 key购买 nike

我正在学习 node.js,需要使用 readline 进行项目。我有以下代码直接来自 readline module example .

const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);

rl.close();
});

但是当我通过node try.js 命令,一直报错如下:

rl.question('What is your favorite food?', (answer) => {
^^
SyntaxError: Unexpected token =>
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3

最佳答案

Arrow functionsECMAScript 6 standard 的新功能之一,仅在 version 4.0.0 中引入 node.js(作为稳定功能) .

您可以升级 node.js 版本或使用旧语法,如下所示:

rl.question('What do you think of Node.js? ', function(answer) {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);

rl.close();
});

(请注意,这些语法之间还有一个区别:this 变量的行为不同。对于这个例子来说无关紧要,但在其他例子中可能。)

关于javascript - Node.js 读取行 : Unexpected token =>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36843574/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com