0

I have json file ( I create this json file using node.js) that I read in HTML using json2html library and if the json file is present / readble then it will be uploaded in our server.

I have json2html code that reads json file and prints HTML page based on content of json file. If json file is unreadable or not present, then I want node.js / javascript to stop or exit the process so nothing gets uploaded to server. How shall I stop / exit the code from processing further if json file is not available.

I do have process.exit() in my HTML file where I read json file . It does not exit gracefully. When I do inspect elements, it errors Uncaught (in promise) ReferenceError: process is not defined. I do have npm i process even though process is global. I still explicitly define const process = require('process'); in my index.js file. Still I get error while opening my html file in browser. How to get past this error. Please refer below for my code from my html file

let responseContent = fetch('studentRecords.json')
        .then(function(response) {
            return response.json();
        })
        .catch(function(err) {
            process.exit(1);
        });

fly.bird
  • 111
  • 3
  • 10
  • You can just log the error, that's why you catch your errors during fetch process. Even if you could kill an entire script execution through non-existing "process" killing your program is not graceful error handling – RoughTomato Jan 14 '20 at 03:37
  • There's no "process" object in browser JavaScript. – Pointy Jan 14 '20 at 03:37
  • 1
    Javascript in your HTML page runs in the browser, not in node.js. The HTML file and any script files are delivered to the browser by node.js, but they run in the browser on the client's computer. They do not run on the server. As such, there is no `process` object in the browser as you can only run browser-based Javascript in the HTML page. The `process` object is only available on your server when running server code. – jfriend00 Jan 14 '20 at 04:15

1 Answers1

1

The problem here is that you are trying to reference process which is a node global variable from the browser in your HTML file. Because of this, it won't exist.

Based on what you've mentioned, you can leave the code block as is and instead of running process.exit(1) you could try logging out an error or wrapping it in a callback function and returning an error instead? That way it won't try to process the fetch response and it will have the option to continue in the way you see fit.

JoelAK
  • 340
  • 2
  • 11