user3799968 user3799968 - 1 year ago 99
Node.js Question

Node cannot find module "fs". what to do

From what I've read, nodejs should contain fs for managing files. However when I require("fs") I get an "Cannot find module "fs"" error. What should I do?

Answer Source

I came across this problem myself when bundling with webpack and found the answer on this thread.

The way to solve it for me was to use the following config:

module.exports = {
  entry: "./app",
  output: {
    path: __dirname,
    filename: "bundle.js"
  module: {
      loaders: [
              test: /\.js$/,
              exclude: 'node_modules',
              loader: 'babel',
              query: {presets: ['es2015']},
  target: 'node'

By setting target to node webpack will make the necessary changes to bundle your node application

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download