0
votes

I am attempting to use fs on in my Javascript code. I have used it before at work and am semi familiar with it, however, when I attempt to use it at home I have to manually (because my project is so small, I am not using grunt or another other aids) set up requirejs. I have tried many examples I could find on-line and I cant seem to get fs to work. In its current implementation I have the code running straight from my JS file. I continually get the:

Module name "fs" has not been loaded yet for context I have checked the require docs and they say to implement these functions: https://requirejs.org/docs/errors.html#notloaded

I have tried every configuration in the docs plus multiple examples that I found online. My file directory is all jacked-up now, but I am back down to one error.

In my js file

require(['file-system'], function (fs) {
    //fs is now loaded.
    console.log('fs', fs);
});

In my html, which has changed over iterations of attempts but resting on this:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <script data-main="jsFile.js" src="scripts/require.js"></script>
    </head>
    <body>
        <p id="cards"></p>
        <p id='POS'></p>
        <script src="jsFile.js"></script>
   </body>
   </html>

The result executes my code with the error of:

Uncaught Error: Module name "fs" has not been loaded yet for context: _. Use >require([]) https://requirejs.org/docs/errors.html#notloaded at makeError (require.js:5) at Object.s [as require] (require.js:5) at requirejs (require.js:5) at file-system.js:5

1

1 Answers

0
votes

The file-system library only runs in Node environments, on the server side or from your command line. So it's not going to be directly accessible from a client-side script the way you seem to be attempting here.

There are other NPM packages that emulate the fs API on the front end, in a browser. But you'll still need to set things up in such a way that the package's source code is available at runtime. This could involve copying scripts from node_modules to a public directory in your server-side code or using a build pipeline tool, such as Gulp.

But, generally speaking, client-side JS running in the browser should not rely upon accessing the external file system at all (with the exception of things like file inputs or other user activity that provides such access).