gpt4 book ai didi

javascript - 使用node.js调用同一目录中的文件时出错

转载 作者:太空宇宙 更新时间:2023-11-04 03:21:12 24 4
gpt4 key购买 nike

更新:我能够在我的代码中找到错误。问题是我没有在 notes.jsconsole.log 中用右括号关闭左括号。

我开始了有关 node.js 的类(class),并且正在学习如何调用从本地系统上的文件创建的其他函数。我有一个目录 node-notes,其中有两个 javascript 文件:app.jsnotes.js。我在 app.js 中调用我的 notes.js。我做了一个简单的 console.log() 调用,只是为了确保它在我的 notes.js 中正常工作。但是,当我尝试使用命令 node app.js 运行代码时,出现以下错误。 enter image description here

更新:我使用的是 Node 10.0.0

我不知道这意味着什么,所以有人可以解释一下错误是什么以及如何修复它。下面我将发布我的 app.jsnotes.js

的代码

app.js

console.log("STARTING app.js")



// calling modules using require
// calling functions in fs module and putting in the
// variable fs

const fs = require('fs');
const os = require('os');
const notes = require('./notes.js');


var user = os.userInfo();
console.log(user);


fs.appendFile('greetings.txt', 'Hello World!' + user.username , function(err){

if(err){
console.log("There was an error")
}else{
console.log("It was successful")
}

});

notes.js

console.log("Calling notes.js")

最佳答案

notes.js 中,您缺少一个 " 来结束字符串。

console.log("调用notes.js)应该是console.log("调用notes.js")

因此你会得到一个SyntaxError:无效或意外的 token

关于javascript - 使用node.js调用同一目录中的文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071162/

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