[英]Why won't fs.readFileSync detect my json file
I'm trying to make a discord bot that can output certain data from my JSON file, and my project structure looks like this.我正在尝试制作一个 discord 机器人,它可以 output 来自我的 JSON 文件的某些数据,我的项目结构如下所示。
Project
|
+-- data/
| |
| +-- compSciCourses.json
|
+-- src/
| |
| +-- search.js
| +-- bot.js
|
+-- test/
| |
| +-- test.js
|
+-- .env
|
+-- package.json
|
+-- package-lock.json
Using search.js, I'm trying to read JSON data with the fs.readFileSync()
like this使用 search.js,我正在尝试像这样使用
fs.readFileSync()
读取 JSON 数据
// src/search.js
const fs = require('fs');
let rawdata = fs.readFileSync("../data/compSciCourses.json")
let courseData = JSON.parse(rawdata)
const findClass = (input) => {
// Some code
}
module.exports = { findClass }
I'm trying to call the function findClass
from my bot.js
我正在尝试从我的
bot.js
调用 function findClass
// src/bot.js
const { Client } = require('discord.js')
const search = require('./search')
const client = new Client()
...
client.on('message', (message) => {
...
if (CMD_NAME === 'find') {
if (args.length === 0)
return message.reply("Please provide course name")
else
return message.reply(search.findClass(args))
}
}
})
...
When I run this with node src/bot.js
, I got this error.当我使用
node src/bot.js
运行它时,出现了这个错误。
fs.js:114
throw err;
^
Error: ENOENT: no such file or directory, open '../data/compSciCourses.json'
at Object.openSync (fs.js:443:3)
at Object.readFileSync (fs.js:343:35)
at Object.<anonymous> (/Users/michaelchang/Desktop/Programming Playground/Machine Learning/ChatBots/discordBot/courseNavigator/src/search.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! coursenavigator@1.0.0 start: `node ./src/bot.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the coursenavigator@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/michaelchang/.npm/_logs/2020-09-01T00_25_12_050Z-debug.log
I don't understand how I get this error because I already specified the JSON file at the right directory.我不明白我是怎么得到这个错误的,因为我已经在正确的目录中指定了 JSON 文件。 Can someone please help me???
有人可以帮帮我吗???
You can resolve the path relative the location of the source file - rather than the current directory - using path.resolve:您可以使用 path.resolve 解析相对于源文件位置的路径 - 而不是当前目录:
const path = require("path");
const file = fs.readFileSync(path.resolve(__dirname, "../data/compSciCourses.json"));
Faced this issue in my project I solved it just by importing the.json file在我的项目中遇到这个问题,我通过导入 .json 文件解决了它
const data = require("../data/compSciCourses.json")
your IDE might give you autocompletion for the path:)你的 IDE 可能会给你自动完成路径:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.