Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why am I getting a NOENT error when I can see the file exactly where it is supposed to be?

I have a need in a node.js app to perform I/O on json files without the benefit of using express routing. This is an API that has only one route (processor.js). It is accessed by a menu selection on the GUI by selecting 'Process'. From that point on everything happens within that route including multiple GETs/PUTs to json (for ids to data and then using the ids to get the data) and the building of SQL rows for populating SQL-Server Tables from the parsed json data. That, at least is the concept I am testing now. It is the hand I have been dealt, so I don't have other options.

I am using fs-extra rather than request or axios etc., because they all seem to expect express routes to accomplish the I/O. I appear to be able to directly read and write the json using fs-extra. I am using sequelize (or will be) for the SQL side.

That's the background.

Here is my processor.js (I am merely validating that I can in fact get idsList returned to me at this point):

'use strict';

//  node_modules
const express = require('express');
const router  = express.Router();
const fse     = require('fs-extra')

//  local modules
const idsList  = require('../functions/getIds');

router.get('/', (req, res) => {
    console.log(idsList);
});


module.exports = router;

Here is my getIds function:

'use strict';

//  library modules
const express      = require('express');
const router       = express.Router();
const fse          = require('fs-extra');
const uri          = require('../uri');

//  initialize general variables
let baseURL = `http://localhost:5000${uri}/`;
let idsID = 'ids.json';

const getIds = async () => {
    let url = `${baseURL}${idsID}`;
    try {
        const idsList = await fse.readJson(url);
        console.log('fse.readJson',idsList);
    } catch (err) {
        console.error(err);
    }
}

module.exports = getIds();

And, here is my error, output to the console:

Listening on port 5000...
{ [Error: ENOENT: no such file or directory, open 
'http://localhost:5000/Users/doug5solas/sandbox/libertyMutual/playground/api/ids.json'] 
errno: -2, 
code: 'ENOENT', 
syscall: 'open', 
path: 'http://localhost:5000/Users/doug5solas/sandbox/libertyMutual/playground/api/ids.json' }

What am I missing?

1 Answer

This problem was solved by using fs.fileReadSync