Webpack - NodeJS - Module not found: Error: Can't resolve 'fs' - node.js

I am getting following error, when build nodejs project with webpack.
ERROR in ./node_modules/mime/mime.js
Module not found: Error: Can't resolve 'fs' in 'C:\Sample\node-ts-sample\node_modules\mime'
# ./node_modules/mime/mime.js 2:9-22
# ./node_modules/send/index.js
# ./node_modules/express/lib/response.js
# ./node_modules/express/lib/express.js
# ./node_modules/express/index.js
# ./src/server.ts
# ./src/index.ts

It was a node application. Added a following node in webpack config solved my issue
target:'node',
I refereed the article at https://jlongster.com/Backend-Apps-with-Webpack--Part-I

Simply add this to your webpack config file:
const webpackConfig = {
target: 'node'
};
module.exports = webpackConfig;

Related

webpack 5 Error "Can't resolve 'uglify-js', '#swc/core', 'esbuild'

I have some errors with webpack 5, express, github actions.
I use the github actions because want to use github secrets.
also, I use webpack because of using the secrets in the code.
anyway, I got errors below,
ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 479:6-26
Module not found: Error: Can't resolve 'uglify-js' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
# ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
# ./webpack.config.js 2:21-53
# ./app.js 13:15-45
ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 557:14-34
Module not found: Error: Can't resolve '#swc/core' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
# ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
# ./webpack.config.js 2:21-53
# ./app.js 13:15-45
ERROR in ./node_modules/terser-webpack-plugin/dist/utils.js 635:18-36
Module not found: Error: Can't resolve 'esbuild' in '/Users/luna/workspace/express_api/node_modules/terser-webpack-plugin/dist'
# ./node_modules/terser-webpack-plugin/dist/index.js 27:4-22
# ./webpack.config.js 2:21-53
# ./app.js 13:15-45
Also, got 15 warnings. if you want to the warnings, let me know.
what is the problem?
Below is the webpack.config.js
const webpack = require("webpack");
module.exports = {
target: "async-node",
mode: "production",
entry: "./app.js",
output: {
path: "/dist",
filename: "main.js",
},
plugins: [
new webpack.DefinePlugin({
"process.env.PA_URL": JSON.stringify(process.env.PA_URL),
"process.env.PA5_EMAIL": JSON.stringify(process.env.PA5_EMAIL),
"process.env.PA5_PASSWORD": JSON.stringify(process.env.PA5_PASSWORD),
}),
],
};
the webpack version is wrong?
You are bundling your entry file, but also any node_modules your ./app.js requires, which in turn tries to require a bunch of third-party stuff, which is not available - hence the errors on missing modules.
Exclude those node_modules by extending your webpack config:
yarn add -D webpack-node-externals
(see: https://www.npmjs.com/package/webpack-node-externals)
const webpack = require("webpack");
const nodeExternals = require("webpack-node-externals");
module.exports = {
target: "async-node",
mode: "production",
entry: "./app.js",
externalsPresets: { node: true }, // <-- here
externals: [nodeExternals()], // <-- and here
...etc
That should exclude them and you should be good to go.

Module not found: Error: Can't resolve 'fs' in '/home/user/Desktop/all/editor/node_modules/winston/dist/winston'

I was trying to add winston logger to the project, but getting these errors:
ERROR in ./node_modules/winston/dist/winston/tail-file.js
Module not found: Error: Can't resolve 'fs' in '/home/user/Desktop/all/editor/node_modules/winston/dist/winston'
# ./node_modules/winston/dist/winston/tail-file.js 9:9-22
# ./node_modules/winston/dist/winston/transports/file.js
# ./node_modules/winston/dist/winston/transports/index.js
# ./node_modules/winston/dist/winston.js
# ./winston.js
# ./client/modules/App/App.jsx
# ./client/routes.jsx
# ./client/index.jsx
# multi core-js/modules/es6.promise core-js/modules/es6.array.iterator webpack-hot-middleware/client react-hot-loader/patch ./client/index.jsx
ERROR in ./node_modules/winston/dist/winston/transports/file.js
Module not found: Error: Can't resolve 'fs' in '/home/user/Desktop/all/editor/node_modules/winston/dist/winston/transports'
# ./node_modules/winston/dist/winston/transports/file.js 33:9-22
# ./node_modules/winston/dist/winston/transports/index.js
# ./node_modules/winston/dist/winston.js
# ./winston.js
# ./client/modules/App/App.jsx
# ./client/routes.jsx
# ./client/index.jsx
# multi core-js/modules/es6.promise core-js/modules/es6.array.iterator webpack-hot-middleware/client react-hot-loader/patch ./client/index.jsx
I have already tried adding node: { fs: 'empty' } in webpack config file and also tried target: 'node' . It's not working.
In my case I was importing winston in a browser environment and winston does not support browser environment yet.
Check shawnxusy's comment here: https://github.com/winstonjs/winston/issues/925

nodejs - Can't resolve 'child_process' with gRPC and webpack

I'm trying to use gRPC with javascript. I want to use the gRPC package, as I need to send some metadata, but I get an error when I'm using require('grpc').
Here's the error message:
Can't resolve 'child_process'
ERROR in ./node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'child_process' in '/home/sglab/dev/grpc-web/client/node_modules/detect-libc/lib'
# ./node_modules/detect-libc/lib/detect-libc.js 4:16-40
# ./node_modules/node-pre-gyp/lib/util/versioning.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/detect-libc/lib'
# ./node_modules/detect-libc/lib/detect-libc.js 5:18-31
# ./node_modules/node-pre-gyp/lib/util/versioning.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/fs.realpath/old.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/fs.realpath'
# ./node_modules/fs.realpath/old.js 24:9-22
# ./node_modules/fs.realpath/index.js
# ./node_modules/glob/glob.js
# ./node_modules/rimraf/rimraf.js
# ./node_modules/node-pre-gyp/lib/util/napi.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/glob/glob.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/glob'
# ./node_modules/glob/glob.js 43:9-22
# ./node_modules/rimraf/rimraf.js
# ./node_modules/node-pre-gyp/lib/util/napi.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/glob/sync.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/glob'
# ./node_modules/glob/sync.js 4:9-22
# ./node_modules/glob/glob.js
# ./node_modules/rimraf/rimraf.js
# ./node_modules/node-pre-gyp/lib/util/napi.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/grpc/index.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/grpc'
# ./node_modules/grpc/index.js 22:9-22
# ./client.js
ERROR in ./node_modules/grpc/src/grpc_extension.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/grpc/src'
# ./node_modules/grpc/src/grpc_extension.js 34:11-24
# ./node_modules/grpc/index.js
# ./client.js
ERROR in ./node_modules/rimraf/rimraf.js
Module not found: Error: Can't resolve 'fs' in '/home/sglab/dev/grpc-web/client/node_modules/rimraf'
# ./node_modules/rimraf/rimraf.js 6:9-22
# ./node_modules/node-pre-gyp/lib/util/napi.js
# ./node_modules/node-pre-gyp/lib/pre-binding.js
# ./node_modules/grpc/src/grpc_extension.js
# ./node_modules/grpc/index.js
# ./client.js
The problems shows when I use this line: var grpc = required('grpc') and this command: npx webpack <file>.
Here's package.json file:
{
"name": "example",
"version": "0.1.0",
"description": "example",
"devDependencies": {
"#grpc/proto-loader": "^0.3.0",
"google-protobuf": "^3.6.1",
"grpc": "^1.24.4",
"grpc-web": "^1.0.0",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0"
}
}
I'm not really familiar with node.js, so it would be great if someone could explain it to me.
Thanks in advance for help!

npm install node-geocoder issue while running npm run build in type script + PCF (Powerapps Component Framework)

I have created a PCF (Powerapps Component Framework) in which I am using the GEOCODE feature. I have installed the required components using "npm install node-geocoder" and have added the below code :
let nodeGeocoder = require('node-geocoder');
let options = {
provider: 'openstreetmap'
};
let geoCoder = nodeGeocoder(options);
geoCoder.geocode('Luray Caverns')
.then((res)=> {
console.log(res[0].latitude);
console.log(res[0].longitude);
})
.catch((err)=> {
console.log(err);
});
When i build this code using "npm run build", the build fails with following error:
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'D:\PCF\Demo\node_modules\request\lib'
Module not found: Error: Can't resolve 'net' in 'D:\PCF\Demo\node_modules\forever-agent'
ERROR in ./node_modules/node-geocoder/lib/geocoder/agolgeocoder.js
Module not found: Error: Can't resolve 'net' in 'D:\PCF\Demo\node_modules\node-geocoder\lib\geocoder'
ERROR in ./node_modules/node-geocoder/lib/geocoder/datasciencetoolkitgeocoder.js
Module not found: Error: Can't resolve 'net' in 'D:\PCF\Demo\node_modules\node-geocoder\lib\geocoder'
ERROR in ./node_modules/node-geocoder/lib/geocoder/abstractgeocoder.js
Module not found: Error: Can't resolve 'net' in 'D:\PCF\Demo\node_modules\node-geocoder\lib\geocoder'
Any help would be helpful.
Thanks in advance.

SlimScroll Error: Module build failed at ng2-slimscroll/index.ts

I have followed below steps Source from https://github.com/jkuri/ng2-slimscroll
Installation
npm install ng2-slimscroll
Imported module like
import { SlimScrollModule } from 'ng2-slimscroll';
imports: [ ....................,
SlimScrollModule,
......................]
facing Error in nodemodules/ng2-slimscroll/index.ts like
ERROR in ./~/ng2-slimscroll/index.ts
Module build failed: Error
at new FatalError (E:\ReconWorkspace\AgreeGateway\node_modules\tslint\lib\error.js:40:23)
at Function.findConfiguration (E:\ReconWorkspace\AgreeGateway\node_modules\tslint\lib\configurat
ion.js:47:15)
at resolveOptions (E:\ReconWorkspace\AgreeGateway\node_modules\tslint-loader\index.js:35:64)
at Object.module.exports (E:\ReconWorkspace\AgreeGateway\node_modules\tslint-loader\index.js:124
:17)
# ./src/main/webapp/app/test/test.module.ts 21:23-48
# ./src/main/webapp/app/app.module.ts
# ./src/main/webapp/app/app.main.ts
# multi (webpack)-dev-server/client?http://localhost:9060 webpack/hot/dev-server ./src/main/webapp/
app/app.main

Resources