Why am I getting a NOENT using Node core module 'fs'












0














This a repeat question (not yet answered) but I have revised and tightened up the code. And, I have included the specific example. I am sorry to keep beating this drum, but I need help.



This is a Node API. I need to read and write JSON data. I am using the Node core module 'fs', not the npm package by the same name (or fs-extra). I have extracted the particular area of concern onto a standalone module that is shown here:



'use strict';

/*==================================================
This service GETs the list of ids to the json data files
to be processed, from a json file with the id 'ids.json'.
It returns and exports idsList (an array holding the ids of the json data files)
It also calls putIdsCleared to clear the 'ids.json' file for the next batch of processing
==================================================*/

// node modules
const fs = require('fs');
const config = require('config');

const scheme = config.get('json.scheme')
const jsonPath = config.get('json.path');
const url = `${scheme}${jsonPath}/`;
const idsID = 'ids.json';
const uri = `${url}${idsID}`;
let idsList = ;

const getList = async (uri) => {
await fs.readFile(uri, 'utf8', (err, data) => {
if (err) {
return(console.log( new Error(err.message) ));
}
return jsonData = JSON.parse(data);
})
}

// The idea is to get the empty array written back to 'ids.json' before returning to 'process.js'
const clearList = async (uri) => {
let data = JSON.stringify({'ids': });
await fs.writeFile(uri, data, (err) => {
if (err) {
return (console.log( new Error(err.message) ));
}
return;
})
}

getList(uri);

clearList(uri)

console.log('end of idsList',idsList);

module.exports = idsList;


Here is the console output from the execution of the module:



    Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
at ReadFileContext.fs.readFile [as callback]

(/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:24:33)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
at fs.writeFile

(/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:36:34)
at fs.js:1167:7
at FSReqWrap.oncomplete (fs.js:141:20)


I am being told there is no such file or directory. However I can copy the uri (as shown in the error message)



File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json


into the search bar of my browser and this is what is returned to me:



{
"ids": [
"5sM5YLnnNMN_1540338527220.json",
"5sM5YLnnNMN_1540389571029.json",
"6tN6ZMooONO_1540389269289.json"
]
}


This result is the expected result. I do not "get" why I can get the data manually but I cannot get it programmatically, using the same uri. What am I missing? Help appreciated.










share|improve this question



























    0














    This a repeat question (not yet answered) but I have revised and tightened up the code. And, I have included the specific example. I am sorry to keep beating this drum, but I need help.



    This is a Node API. I need to read and write JSON data. I am using the Node core module 'fs', not the npm package by the same name (or fs-extra). I have extracted the particular area of concern onto a standalone module that is shown here:



    'use strict';

    /*==================================================
    This service GETs the list of ids to the json data files
    to be processed, from a json file with the id 'ids.json'.
    It returns and exports idsList (an array holding the ids of the json data files)
    It also calls putIdsCleared to clear the 'ids.json' file for the next batch of processing
    ==================================================*/

    // node modules
    const fs = require('fs');
    const config = require('config');

    const scheme = config.get('json.scheme')
    const jsonPath = config.get('json.path');
    const url = `${scheme}${jsonPath}/`;
    const idsID = 'ids.json';
    const uri = `${url}${idsID}`;
    let idsList = ;

    const getList = async (uri) => {
    await fs.readFile(uri, 'utf8', (err, data) => {
    if (err) {
    return(console.log( new Error(err.message) ));
    }
    return jsonData = JSON.parse(data);
    })
    }

    // The idea is to get the empty array written back to 'ids.json' before returning to 'process.js'
    const clearList = async (uri) => {
    let data = JSON.stringify({'ids': });
    await fs.writeFile(uri, data, (err) => {
    if (err) {
    return (console.log( new Error(err.message) ));
    }
    return;
    })
    }

    getList(uri);

    clearList(uri)

    console.log('end of idsList',idsList);

    module.exports = idsList;


    Here is the console output from the execution of the module:



        Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
    at ReadFileContext.fs.readFile [as callback]

    (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:24:33)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
    Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
    at fs.writeFile

    (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:36:34)
    at fs.js:1167:7
    at FSReqWrap.oncomplete (fs.js:141:20)


    I am being told there is no such file or directory. However I can copy the uri (as shown in the error message)



    File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json


    into the search bar of my browser and this is what is returned to me:



    {
    "ids": [
    "5sM5YLnnNMN_1540338527220.json",
    "5sM5YLnnNMN_1540389571029.json",
    "6tN6ZMooONO_1540389269289.json"
    ]
    }


    This result is the expected result. I do not "get" why I can get the data manually but I cannot get it programmatically, using the same uri. What am I missing? Help appreciated.










    share|improve this question

























      0












      0








      0







      This a repeat question (not yet answered) but I have revised and tightened up the code. And, I have included the specific example. I am sorry to keep beating this drum, but I need help.



      This is a Node API. I need to read and write JSON data. I am using the Node core module 'fs', not the npm package by the same name (or fs-extra). I have extracted the particular area of concern onto a standalone module that is shown here:



      'use strict';

      /*==================================================
      This service GETs the list of ids to the json data files
      to be processed, from a json file with the id 'ids.json'.
      It returns and exports idsList (an array holding the ids of the json data files)
      It also calls putIdsCleared to clear the 'ids.json' file for the next batch of processing
      ==================================================*/

      // node modules
      const fs = require('fs');
      const config = require('config');

      const scheme = config.get('json.scheme')
      const jsonPath = config.get('json.path');
      const url = `${scheme}${jsonPath}/`;
      const idsID = 'ids.json';
      const uri = `${url}${idsID}`;
      let idsList = ;

      const getList = async (uri) => {
      await fs.readFile(uri, 'utf8', (err, data) => {
      if (err) {
      return(console.log( new Error(err.message) ));
      }
      return jsonData = JSON.parse(data);
      })
      }

      // The idea is to get the empty array written back to 'ids.json' before returning to 'process.js'
      const clearList = async (uri) => {
      let data = JSON.stringify({'ids': });
      await fs.writeFile(uri, data, (err) => {
      if (err) {
      return (console.log( new Error(err.message) ));
      }
      return;
      })
      }

      getList(uri);

      clearList(uri)

      console.log('end of idsList',idsList);

      module.exports = idsList;


      Here is the console output from the execution of the module:



          Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
      at ReadFileContext.fs.readFile [as callback]

      (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:24:33)
      at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
      Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
      at fs.writeFile

      (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:36:34)
      at fs.js:1167:7
      at FSReqWrap.oncomplete (fs.js:141:20)


      I am being told there is no such file or directory. However I can copy the uri (as shown in the error message)



      File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json


      into the search bar of my browser and this is what is returned to me:



      {
      "ids": [
      "5sM5YLnnNMN_1540338527220.json",
      "5sM5YLnnNMN_1540389571029.json",
      "6tN6ZMooONO_1540389269289.json"
      ]
      }


      This result is the expected result. I do not "get" why I can get the data manually but I cannot get it programmatically, using the same uri. What am I missing? Help appreciated.










      share|improve this question













      This a repeat question (not yet answered) but I have revised and tightened up the code. And, I have included the specific example. I am sorry to keep beating this drum, but I need help.



      This is a Node API. I need to read and write JSON data. I am using the Node core module 'fs', not the npm package by the same name (or fs-extra). I have extracted the particular area of concern onto a standalone module that is shown here:



      'use strict';

      /*==================================================
      This service GETs the list of ids to the json data files
      to be processed, from a json file with the id 'ids.json'.
      It returns and exports idsList (an array holding the ids of the json data files)
      It also calls putIdsCleared to clear the 'ids.json' file for the next batch of processing
      ==================================================*/

      // node modules
      const fs = require('fs');
      const config = require('config');

      const scheme = config.get('json.scheme')
      const jsonPath = config.get('json.path');
      const url = `${scheme}${jsonPath}/`;
      const idsID = 'ids.json';
      const uri = `${url}${idsID}`;
      let idsList = ;

      const getList = async (uri) => {
      await fs.readFile(uri, 'utf8', (err, data) => {
      if (err) {
      return(console.log( new Error(err.message) ));
      }
      return jsonData = JSON.parse(data);
      })
      }

      // The idea is to get the empty array written back to 'ids.json' before returning to 'process.js'
      const clearList = async (uri) => {
      let data = JSON.stringify({'ids': });
      await fs.writeFile(uri, data, (err) => {
      if (err) {
      return (console.log( new Error(err.message) ));
      }
      return;
      })
      }

      getList(uri);

      clearList(uri)

      console.log('end of idsList',idsList);

      module.exports = idsList;


      Here is the console output from the execution of the module:



          Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
      at ReadFileContext.fs.readFile [as callback]

      (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:24:33)
      at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:235:13)
      Error: ENOENT: no such file or directory, open 'File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
      at fs.writeFile

      (/Users/doug5solas/sandbox/libertyMutual/server/.playground/ids.js:36:34)
      at fs.js:1167:7
      at FSReqWrap.oncomplete (fs.js:141:20)


      I am being told there is no such file or directory. However I can copy the uri (as shown in the error message)



      File:///Users/doug5solas/sandbox/libertyMutual/server/api/ids.json


      into the search bar of my browser and this is what is returned to me:



      {
      "ids": [
      "5sM5YLnnNMN_1540338527220.json",
      "5sM5YLnnNMN_1540389571029.json",
      "6tN6ZMooONO_1540389269289.json"
      ]
      }


      This result is the expected result. I do not "get" why I can get the data manually but I cannot get it programmatically, using the same uri. What am I missing? Help appreciated.







      node.js json fs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 18:01









      doug5solas

      308




      308
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Your File URI is in the wrong format.
          It shouldn't contain the File:// protocol (that's a browser-specific thing).



          I'd imagine you want C://Users/doug5solas/sandbox/libertyMutual/server/api/ids.json.






          share|improve this answer





















          • The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
            – doug5solas
            Nov 20 at 20:21










          • Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
            – doug5solas
            Nov 20 at 20:42





















          0














          I solved the problem by going to readFileSync. I don't like it but it works and it is only one read.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398898%2fwhy-am-i-getting-a-noent-using-node-core-module-fs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your File URI is in the wrong format.
            It shouldn't contain the File:// protocol (that's a browser-specific thing).



            I'd imagine you want C://Users/doug5solas/sandbox/libertyMutual/server/api/ids.json.






            share|improve this answer





















            • The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
              – doug5solas
              Nov 20 at 20:21










            • Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
              – doug5solas
              Nov 20 at 20:42


















            0














            Your File URI is in the wrong format.
            It shouldn't contain the File:// protocol (that's a browser-specific thing).



            I'd imagine you want C://Users/doug5solas/sandbox/libertyMutual/server/api/ids.json.






            share|improve this answer





















            • The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
              – doug5solas
              Nov 20 at 20:21










            • Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
              – doug5solas
              Nov 20 at 20:42
















            0












            0








            0






            Your File URI is in the wrong format.
            It shouldn't contain the File:// protocol (that's a browser-specific thing).



            I'd imagine you want C://Users/doug5solas/sandbox/libertyMutual/server/api/ids.json.






            share|improve this answer












            Your File URI is in the wrong format.
            It shouldn't contain the File:// protocol (that's a browser-specific thing).



            I'd imagine you want C://Users/doug5solas/sandbox/libertyMutual/server/api/ids.json.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 18:34









            Simon Poole

            307213




            307213












            • The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
              – doug5solas
              Nov 20 at 20:21










            • Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
              – doug5solas
              Nov 20 at 20:42




















            • The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
              – doug5solas
              Nov 20 at 20:21










            • Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
              – doug5solas
              Nov 20 at 20:42


















            The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
            – doug5solas
            Nov 20 at 20:21




            The reason I used "Files://..." was a desperation move because "localhost:3000/..." failed both programmatically and manually. But, I wasn't aware that "Files:///..." was browser specific, so I will change the way I construct the uri. That was a good piece of info to pick up. And, I believe 'C://...' will not work on my Mac.
            – doug5solas
            Nov 20 at 20:21












            Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
            – doug5solas
            Nov 20 at 20:42






            Here is the result using localhost: Error: ENOENT: no such file or directory, open 'localhost:3000//Users/doug5solas/sandbox/libertyMutual/server/api/ids.json'
            – doug5solas
            Nov 20 at 20:42















            0














            I solved the problem by going to readFileSync. I don't like it but it works and it is only one read.






            share|improve this answer


























              0














              I solved the problem by going to readFileSync. I don't like it but it works and it is only one read.






              share|improve this answer
























                0












                0








                0






                I solved the problem by going to readFileSync. I don't like it but it works and it is only one read.






                share|improve this answer












                I solved the problem by going to readFileSync. I don't like it but it works and it is only one read.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 at 17:58









                doug5solas

                308




                308






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398898%2fwhy-am-i-getting-a-noent-using-node-core-module-fs%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen