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 loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}, { test: /\.json$/, loader: 'json-loader' }, node: { fs: "empty"

And I want to read a file using fs

I am doing something like:

var fs = require('fs')
console.log(fs)
fs.readFile('input.txt', function (err, buffer) {
        console.log("buffer")
        console.log(buffer)

I just want to read a file here but when I do this it gives me error saying:

fs.readFile is not a function

I have installed fs using npm install --save fs

Also when I print fs it gives me empty object. Above I have done console.log(fs) it is giving me empty object

What is wrong in here?

Remove node: { fs: "empty" } and add target: 'node': webpack.github.io/docs/configuration.html#target – Bob Sponge May 19, 2016 at 9:40 You trying to run this code from browser? In browser, obviously, there is not any access to file system. – Bob Sponge May 19, 2016 at 9:54

Needs to be removed. Moreover, all your code must run on the server. You cannot read files like this in the browser. Node APIs are server-side only, so you would need to build an API using express or some similar library

    router.get('/file/read', (req, res, next) => { fs.readFile('input.txt', function (err, buffer) {
            console.log("buffer")
            res.send(buffer);

Then in your browser you would need to use AJAX to call the API and retrieve the file contents.

Reading arbitrary files has OBVIOUS SECURITY ISSUES . You should not allow the api to read any files the user likes. You should be very careful here to restict input and limit the set of files available.

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.