readFileSync is not a function

后端 未结 1 1731
一整个雨季
一整个雨季 2020-12-14 06:59

I am relatively new to Node.js and have been looking around but cannot find a solution. I did check the require javascript file and it does not seem to have a method for \"r

相关标签:
1条回答
  • 2020-12-14 07:26

    Node.js does not use Require.js. Require.js was built so that you could have asynchronous module loading on the client-side (in your browser).

    Node.js uses CommonJS style modules. Your code using CommonJS would look like this:

    var fs = require('fs');
    console.log("\n *STARTING* \n");
    var contents = fs.readFileSync("sliderImages", "utf8");
    

    If we assume you saved this in a file called main.js you would then enter this command in your console (make sure you are in the same directory as the file):

    node main.js
    

    This code will not run in the browser. Node.js runs on the server. If you want to load a JSON file on the browser side then you'll need to load it using AJAX. There are numerous resources available to show you how to do this. Be aware that you must either run your page from a server or have a special flag enabled to load in files from the file system.

    0 讨论(0)
提交回复
热议问题