Categories
JavaScript Answers

How to fix Node cannot find module “fs” when using Webpack?

Spread the love

To fix Node cannot find module "fs" when using Webpack, we set target to 'node'.

For instance, in webpack.config.js, we write

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

to set target to 'node' to make Webpack build for Node.js instead of browser.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *