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',
}
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With