A
A
Anton P.2018-05-22 21:02:58
JavaScript
Anton P., 2018-05-22 21:02:58

What causes the "Can't resolve 'fs'" error when using node.js and wabpack?

Task: Need to get files from .
Code snippet written by me

var fs = require('fs');
let path = require('path');

module.exports = {
  home: require('../components/Home.js'),
  contact: require('../components/Contact.js'),
  path: function(){
    let p = path.resolve(__dirname,'./src/components');
    fs.readdir( p, function(err, items) {
      console.log(err);
      console.log(items);
    });
  },
}

Error while building project
ERROR in ./src/core/registry.js
Module not found: Error: Can't resolve 'fs' in '/var/www/swylet.ru/src/core'
 @ ./src/core/registry.js 1:9-22
 @ ./src/core/router.js
 @ ./src/core/boot.js
 @ ./src/index.js

webpack.config.js I have the following
let path = require('path');
let config = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname,'./www/'),
    filename: 'script.js'
  },
  devtool: "eval-sourcemap"
};
module.exports = (evn, options) => {
  let mode = options.mode === 'production';
  config.devtool = mode ? false : "eval-sourcemap";
  return config
};

Googling, I found that some people get rid of this problem by adding
node: {
fs: "empty"
} to webpack.config
I tried it, the build went fine, but in the browser I get an error
TypeError: fs.readdir is not a function
What am I doing wrong ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question