I was requiring some data from a .json file but I am getting this error:
Error: ENOENT: no such file or directory, open '../Jsons/eshop.json'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.execute (C:\Users\Pooyan\Desktop\PDM Bot Main\commands\shop.js:9:24)
at module.exports (C:\Users\Pooyan\Desktop\PDM Bot Main\events\guild\message.js:114:15)
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '../Jsons/eshop.json'
}
My code:
let shop_data = JSON.parse(Buffer.from(fs.readFileSync('../Jsons/eshop.json')).toString());
let index = (args[0] || "1");
let page = shop_data.pages[index];
I think that's all you need, but if any other code was needed, comment it.
I am using discord.js v13 and node.js 16
The issue is with the path. It seems the path is invalid for the given eshop.json file or there might be any spelling mistake in the path.
fs.readFileSync takes the relative path:
test.js
JSON
sampleJSON
eshop.json
fs.readFileSync('./JSON/sampleJSON/eshop.json');
You need to use path module and join the folder names to define the json file location.
Let,the data is in following directory. ( NewProject/data/eshop.json )
NewProject
Project root directory
data
eshop.json
You want to access the eshop.json file from any file under the project directory.
Solution is below:
const fs = require('fs')
const path = require('path');
const directory = path.join('data', 'eshop.json')
// directory ==> data\eshop.json
exports.getRandomUserService = () => {
const jsonData = fs.readFileSync(directory);
const eshop= JSON.parse(jsonData);
console.log('eshop data from json file: ',eshop);
}
Related
error image: (https://i.stack.imgur.com/IesaI.jpg)
I tried to run the code but it resulted in an error that I could not open the json file saying that it did not exist
Because it can't find the file you want. You should give the absolute path to file.
const fs = require('fs');
const path = require('path');
fs.readFile(path.join(__dirname, 'database/registros.json'), {encoding: 'utf-8'}, function(err,data) {});
I'm building a static blog using Nextjs 13 and deploying it on Vercel. I builded and started the project locally and everything was working, but on Vercel I got this error:
ERROR Error: ENOENT: no such file or directory, open 'posts/second.md' at Object.openSync (node:fs:600:3) at Object.readFileSync (node:fs:468:35) at getPostBySlug (/var/task/.next/server/chunks/207.js:146:63) at Post (/var/task/.next/server/app/posts/[slug]/page.js:603:52) at T (/var/task/.next/server/chunks/760.js:11441:25) at Ma (/var/task/.next/server/chunks/760.js:11604:33) at Array.toJSON (/var/task/.next/server/chunks/760.js:11397:32) at stringify () at V (/var/task/.next/server/chunks/760.js:11696:53) at ping (/var/task/.next/server/chunks/760.js:11496:43) { errno: -2, syscall: 'open', code: 'ENOENT', path: 'posts/second.md'}
The error happens when I go to the "/posts/second" route for example, not in the main page
This is the code interested:
const getPostBySlug = (slug: string) => {
const folder = "posts/";
const file = `${folder}${slug}.md`;
const content = fs.readFileSync(file, "utf8");
return matter(content)
};
The posts folder is located in the root folder.
I tried to modify the next config by adding the output option and setting it to 'standalone':
const nextConfig = {
// config
output: 'standalone',
}
I also tried to modify the path to locate the folder but nothing seems to work.
If more information is needed. project is published on GitHub
I solved my problem by looking at a tutorial provided by Vercel, so thanks to this line of code path.join(process.cwd(), 'posts'); the folder path is resolved.
I have a simple NextJS API page which just fetches a JSON file from one of the node_modules and shows the content at http://localhost:3000/api/my-json. This is the code:
// Path: /pages/api/my-json.ts
import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import { promises as fs } from "fs";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
//Find the absolute path of the json directory
const jsonLocation = path.join(
process.cwd(),
"node_modules/#my-company/my-package/lib/my-json-file.json"
);
//Read the json data file data.json
const fileContents = await fs.readFile(jsonLocation, "utf8");
//Return the content of the data file in json format
res.status(200).json(fileContents);
}
It works fine in dev mode, also works when building a production NextJS app by running:
yarn install
yarn build
yarn start
But when I upload the code to Vercel, the app is unable to find the json file, and my page https://my-app.vercel.app/api/my-json returns 500 error:
2022-12-12T10:25:14.142Z 04b6a8fe-53f7-46b6-8c49-98dadf358eb3 ERROR [Error: ENOENT: no such file or directory, open '/var/task/node_modules/#my-company/my-package/lib/my-json-file.json'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/var/task/node_modules/#my-company/my-package/lib/my-json-file.json'
}
Somebody had this problem before? Do I need to copy that JSON file manually somehow during Vercel deployment? How comes it works on local builds but not on Vercel?
so I'm trying to set up a very basic tfjs project but am stuck in an early part of getting it running. Running into a file path not found error: 'Error: ENOENT: no such file or directory'. My code looks like this:
const tf = require('#tensorflow/tfjs')
require('#tensorflow/tfjs-node')
async function test_function() {
const house_sales_dataset = tf.data.csv('file://./datasets/kc_house_data.csv');
const sample = house_sales_dataset.take(10);
try {
const dataArray = await sample.toArray();
} catch(err) {
console.log(err)
}
}
test_function();
My file structure for the npm project is the following:
Root project directory is called 'tensorflow-practice'. Inside this is app.js, package.json, package-lock.json, node-modules, and a folder called 'datasets'. Inside 'datasets' folder is the .csv file called 'kc_house_data.csv'.
https://imgur.com/a/jWLr3wt
Not sure why this isn't working...any help much appreciated.
I tried to uplaod file and move to new directory already exists.
follow Writing files in Node.js but I got the error:
Error: EISDIR, open '/Users/name/Sites/project/app/assets/images/UploadTemporary/'
at Error (native)
and I found Using Node.js I get, "Error: EISDIR, read" and Node.js Error: EISDIR, open Error similar error message, my UploadTemporary folder already exists do I mess something wrong?
I don't get it, if its not a directory what else can be?
var multipart = require('connect-multiparty');
var fs = require('fs');
var path = require('path');
var appDir = path.dirname(require.main.filename);
...
var sourceFile = req.files.file[0].path;
var destinationFile = appDir + '/assets/images/UploadTemporary/';
var source = fs.createReadStream(sourceFile);
var destination = fs.createWriteStream(destinationFile);
source.pipe(destination);
source.on('end', function () {
fs.unlinkSync(sourceFile);
});
When you are writing a file to a specific directory, you need to give the actual destination file name as well. Unlike cp command, the destination filename will not be inferred by fs module.
In your case, you are trying to write to a directory, instead of a file. That is why you are getting EISDIR error. To fix this, as you mentioned in the comments,
var destinationFile = appDir + '/assets/images/UploadTemporary/' + newfilename;
include the file name as well.