Home > Software engineering >  ENOENT: no such file or directory, open './data/databases.json'
ENOENT: no such file or directory, open './data/databases.json'

Time:08-01

am trying to write json data from a file contained in model folder to another file inside data folder and the two folders are contained in server folder. but i keep on getting the error message that there is no such file directory. the code below is inside model/model.ts file and the file i want to write to is in data/databases.json and both are inside server folder. i have tried using the following path ./data/databases.json, ../data/databases.json but still not working. the folder is server/models/model.ts

let Products = require("../data/product.json") // i was able to import from the same file can't write to it using the same path
const fs = require("fs")

const creat = (newInfo:any) => {
    return new Promise((resolve,reject)=>{
        let id = companyInfo.length 1
        let  value = {...newInfo,"id":id}
        companyInfo.push(value)
        fs.writeFileSync('../data/databases.json',JSON.stringify(companyInfo, null, 2))
        resolve(value)
    })
}
export {creat}

i have another file where i call the above function and the folder path is server/controller/controller.ts and below is the code

import {creat} from "../models/model"   // importing the function here

import http, { IncomingMessage, Server, ServerResponse } from "http";

const creatCompanyinfo = async (req:IncomingMessage,res:ServerResponse)=>{
    let body =""
   req.on("data",(chunk)=>{
       body  = chunk.toString()
   })
   req.on("end",async ()=>{
      let newInfo = JSON.parse(body)
     
      let result = await creat(newInfo)
      res.setHeader("Content-Type","application/json")
      res.end(JSON.stringify(result)) 
   })
}
export {creatCompanyinfo}

and finally the last file that handles the routing and the path is server/app.ts. below is the code

import http, { IncomingMessage, Server, ServerResponse } from "http";

import {creatCompanyinfo} from "./controller/controller" //importing the function here

const server: Server = http.createServer((req: IncomingMessage, res: ServerResponse) => {
 if(req.url === "/companies" && req.method==="POST"){
       creatCompanyinfo(req,res)
     }
}

this is the path to the json file am writting to server/data/databases.json

below is the file structure

   `server/`
        `controller/controller.ts`
        `data/databases.json`
        `model/model.ts`
        `app.ts`
    

below is the error message am getting

Server Running at Port 4000.....
node:fs:585
  handleErrorFromBinding(ctx);
  ^

Error: ENOENT: no such file or directory, open './data/databases.json'
    at Object.openSync (node:fs:585:3)
    at Object.writeFileSync (node:fs:2170:35)
    at /Users/dec/Challenges/week-5-task-stanzealot/server/lib/models/model.js:28:12
    at new Promise (<anonymous>)
    at Object.creat (/Users/dec/Challenges/week-5-task-stanzealot/server/lib/models/model.js:24:12)
    at IncomingMessage.<anonymous> (/Users/dec/Challenges/week-5-task-stanzealot/server/lib/controller/controller.js:44:36)
    at IncomingMessage.emit (node:events:539:35)

CodePudding user response:

Behavior of relative path in import() is different from relative path in fs methods.

import path is relative to source file, but fs path is relative to working directory.

Try converting your path to an absolute path:

const path = require("path");
const aPath = path.resolve(__dirname, '../data/databases.json')
fs.writeFileSync(aPath ,JSON.stringify(companyInfo, null, 2))
  • Related