49

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70
  • 1
    `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 '16 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 '16 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 '16 at 16:04
  • Is there a reason you are using requirejs in node? If there is, are you importing it? – figgyc May 24 '16 at 16:05
  • I dont have a file called fs, is that a problem? Again I do not know where to get this file. – L1ghtk3ira May 24 '16 at 16:05
  • I am just trying to read a json file using node. – L1ghtk3ira May 24 '16 at 16:05
  • 1
    There is no file for `fs`, it's a built in module that is always there – adeneo May 24 '16 at 16:06
  • How are you running this file? Are you running it from the command prompt? – Mike Cluck May 24 '16 at 16:07
  • Sounds like the code is running in a browser, with requireJS, that would produce the error `Module name "fs" has not been loaded yet...`, it has to run ***in*** NodeJS. – adeneo May 24 '16 at 16:09
  • I am doing them in a javascript page that is loaded by the browser – L1ghtk3ira May 24 '16 at 16:10
  • The browser doesn't have access to the file system, and it's ***not*** NodeJS either, so `fs` can't be used in a browser. – adeneo May 24 '16 at 16:13

2 Answers2

24

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.

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • 2
    Thank you I was a bit un certain how to use node.js, that cleared a lot up, thank you! – L1ghtk3ira May 24 '16 at 16:16
  • It seems to me that RequireJS supports Node?: https://requirejs.org/docs/node.html – kip2 Oct 07 '20 at 22:02
  • @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 08 '20 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 08 '20 at 19:21
2

This error can arise if you write

const fs = import('fs');

in a Node module

but should write

import fs from 'fs';

David
  • 895
  • 8
  • 12