Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 "readFileSync". Perhaps I don't have a proper require file? I had a hard time finding this file, everywhere talked about it but most people did not post where to get it.

I installed Node.js and have the require.js file. My current code is like this:

fs = require(['require'], function (foo) {
//foo is now loaded.
console.log("\n *STARTING* \n");
// Get content from file
var contents = fs.readFileSync("sliderImages", 'utf8');

I had a bit at first getting require to work however it seems to load the require JavaScript file. I have been following guides and I am not sure why I get this error:

Uncaught TypeError: fs.readFileSync is not a function

I have tried many fixes and cannot seem to figure this one out.

fs obviously isn't Node's "fs", otherwise the readFileSync method wouldn't be undefined, so you're doing something wrong with your require? Why wouldn't you just do var fs = require('fs') – adeneo May 24, 2016 at 16:01 Don't use Require.js with Node. Node has it's own way of handling modules using CommonJS. As adeneo said, just do var fs = require('fs'). Require.js is for module loading on the client-side. – Mike Cluck May 24, 2016 at 16:03 When I tried using that I got the following error: Module name "fs" has not been loaded yet for context: _. Use require([]). So I began looking for a solution. – L1ghtk3ira May 24, 2016 at 16:04

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.

@kip2 RequireJS does work in a Node environment but Node does not support RequireJS style modules by default. Node handles modules in its own way. There's no reason to use RequireJS in Node, especially since client-side bundling has advanced a lot since the time that RequireJS was built – Mike Cluck Oct 8, 2020 at 16:44 Hi @MikeCluck, my situation isn't exactly like OP's. I'm trying to use a node module in a script that's supposed to run browser/client side, by doing import config from './config/env_config' ,but was getting this FileSystem.readFileSync is not a function error. env_config is actually just a wrapper around node-config that I use to manage environment config values – kip2 Oct 8, 2020 at 19:21

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.