Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node cannot find module “fs” when using webpack

I'm using node.js (the last version) and webpack to create a bundle. From what I've read, node.js should contain fs module for managing files. However when I call require("fs") I get an Cannot find module "fs" error. I've stuck with 'require is not defined ' in my console after adding "target: 'node' ". Any help will be usefull, thx.

var webpack = require('webpack');

module.exports = {
    entry: "./client/main.js",
    output: {
        path: __dirname + '/public/build/',
        publicPath: "build/",
        filename: "bundle.js"
    },

    node: {fs: "empty"},

    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: "babel",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader!autoprefixer-loader",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.less$/,
                loader: "style-loader!css-loader!autoprefixer-loader!less",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.gif$/,
                loader: "url-loader?limit=10000&mimetype=image/gif"
            },
            {
                test: /\.jpg$/,
                loader: "url-loader?limit=10000&mimetype=image/jpg"
            },
            {
                test: /\.png$/,
                loader: "url-loader?limit=10000&mimetype=image/png"
            },
            {
                test: /\.svg/,
                loader: "url-loader?limit=26000&mimetype=image/svg+xml"
            },
            {
                test: /\.jsx$/,
                loader: "react-hot!babel",
                exclude: [/node_modules/, /public/]
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            }
        ]
    },
    target: 'node',
}
like image 408
Микола Стельмах Avatar asked Nov 30 '16 20:11

Микола Стельмах


People also ask

How do I use TypeScript FS?

To import and use the fs module in TypeScript, make sure to install the type definitions for node by running npm i -D @types/node and import fs with the following line import * as fs from 'fs' , or import { promises as fsPromises } from 'fs' if using fs promises.

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

Which of the following is true about node js?

True! Node js is a single threaded application but it support concurrency via concept of event and callbacks. Q 22 - Which of the following is true with respect to Node. A - Every API of Node js are asynchronous.


2 Answers

By setting node: {fs: "empty"}, you are telling webpack that when importing fs an empty object should be returned. Remove that and it should work fine. https://webpack.js.org/configuration/node/#node

like image 170
pizzarob Avatar answered Oct 18 '22 14:10

pizzarob


Instead of marking "fs" as empty, try to decorate it like this:

externals:{
    "fs": "commonjs fs"
}

That should tell webpack to load it as a module instead of considering it an environment object/ variable.

Ref: Node cannot find module "fs" when using webpack

like image 22
PDG Avatar answered Oct 18 '22 14:10

PDG