css-loader: Can't resolve 'fs' when bundle with webpack
Do you want to request a feature or report a bug? Bug
What is the current behavior?
Webpack finish bundle without errors with css-loader 0.26.4
.
After update to 0.27.0
webpacking finished with error:
ERROR in ./~/convert-source-map/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Projects\project_name\node_modules\convert-source-map'
@ ./~/convert-source-map/index.js 2:9-22
@ ./~/css-loader/lib/css-base.js
If the current behavior is a bug, please provide the steps to reproduce.
- Update css-loader from
0.26.4
to0.27.0
version - Run webpack to build
What is the expected behavior? Works without error
Please mention other relevant information such as your webpack version, Node.js version and Operating System. Windows 8.1 Node.js 7.7.2 Webpack 2.2.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 455
- Comments: 101 (12 by maintainers)
Links to this issue
Commits related to this issue
- fix: Can't resolve 'fs' when bundle with webpack #447 This is a webpack core level problem. Hackfixing for now. Closes #447. — committed to webpack-contrib/css-loader by bebraw 7 years ago
- Try to use workbox in main.js Per this instruction (https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW) but results in error. Error is similar to: https://github.com/w... — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- Fix: fix 'can't resolve fs' error Used simple workaround Issue: https://github.com/webpack-contrib/css-loader/issues/447 — committed to c0ntrast3d/bpmn-js_modeler_webpack by c0ntrast3d 7 years ago
- Try to use workbox in main.js Per this instruction (https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW) but results in error. Error is similar to: https://github.com/w... — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- Try to use workbox in main.js Per this instruction (https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW) but results in error. Error is similar to: https://github.com/w... — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- Try to use workbox in main.js Per this instruction (https://workboxjs.org/reference-docs/latest/module-workbox-build.html#.generateSW) but results in error. Error is similar to: https://github.com/w... — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- work around the 'fs' issue https://github.com/webpack-contrib/css-loader/issues/447 — committed to uicestone/pbjd-www by uicestone 6 years ago
- add dotenv-webpack I took this bug => https://github.com/webpack-contrib/css-loader/issues/447 — committed to ISC-MakeIT/f_college_sns by fumihumi 6 years ago
- Basic foundation code for Speck component; currently giving error upon execution 'fs.readFileSync is not a function'. — committed to plotly/dash-bio by deleted user 6 years ago
- Fix webpack error according to https://github.com/webpack-contrib/css-loader/issues/447 — committed to loomnetwork/loom-js by gakonst 6 years ago
- Fix webpack error according to https://github.com/webpack-contrib/css-loader/issues/447 — committed to loomnetwork/loom-js by gakonst 6 years ago
- Fixed cannot resolve module errors https://github.com/broofa/node-mime/issues/197 https://github.com/webpack-contrib/css-loader/issues/447 — committed to gcalica/YaBoiLight by gcalica 5 years ago
- [Feature] add ExcelDownloadButton (2) 까먹고 커밋 안 한 것들 - xlsx package에서 fs 모듈관련 이슈가 있어서 조치 [참고](https://github.com/webpack-contrib/css-loader/issues/447) - add DateParser:getDateTimeNumbers, getDateTimeS... — committed to DEV-MUGLES/pickk-admin by greatSumini 4 years ago
- Be able to build browser target. Fix fs in webpack - some ref: https://github.com/webpack-contrib/css-loader/issues/447 — committed to geostyler/geostyler-mapfile-parser by ger-benjamin 4 years ago
- Webpack shinanigans - see linked issue The fs module in the dependancy was causing issue due to the [webpack issue](https://github.com/webpack-contrib/css-loader/issues/447#issuecomment-285598881) in... — committed to Quinn-Donnelly/automation-discord-bot by Quinn-Donnelly 4 years ago
Adding
to webpack config file and the error is gone. Not sure how it works, It seems like webpack did some thing with
fs
module inweb
target.The issue has been acknowledged and we are in process of figuring out a good fix. No need for "me too"s. Please add a thumbs up on the original issue if you want to do that. 👍
add the following to your Webpack config:
@PredokMiF You don’t unpublish anything, ever.
The workaround is sufficient and the bug is actually in webpack, related to
fs
in the web target.WORKAROUND thanks to @hdnha11 / @mehdivk
If using this workaround isn’t viable, version lock to the previous minor version.
same here
For
Webpack >5
You shoud add a block towebpack.config.js
like this:just wanted to add that I fixed this by the answer in https://github.com/josephsavona/valuable/issues/9 about setting
target: 'node'
This explains more: http://jlongster.com/Backend-Apps-with-Webpack--Part-I
Same for me, had to rollback that update
same here
Did you can to unpublish this version? It will keep calm to all of us
Someone please fix this issue. Non of the mentioned answer could fix it for me
Fixed by adding
target: 'node',
in webpack.config.js@Percapio add this
I get an error like this:
TypeError: fs.readFileSync is not a function
I fixed the problem by changing webpack.config.js:
… var config = Encore.getWebpackConfig(); config.node = { fs: ‘empty’ }; module.exports = config;
and: yarn add --dev file system
same here
Still i am unable to resolve the fs error plz help me
I added
node: { fs: 'empty' }
without luck,then I added --config to start command: ̀webpack-dev-sever webpack.config.dev.js`
You must add the --config flag to put in a custom file. ̀webpack-dev-sever --config webpack.config.dev.js`
Boom! It worked.
Me Too. This is still an issue
Same problem - why is this issue closed? Should I 👍 a different issue?
How should I go about solving this issue when importing an npm module that uses Webpack (i.e., when importing an npm module that imports a package that yields this problem)?
To add some clarity: say I’ve create two npm modules, both using Webpack. Let’s call them module A and module B.
Module A imports
require
, which yields this error. Thus, inwebpack.config.js
of module A, I add:Module B then imports module A. However, even though I’ve added the above code in module A, the error still comes up when I try to build module B.
Does anyone know how to solve this problem without adding the above code to module B as well (i.e., modifying only module A)?
I got this problem because I ran:
webpack webpack.config.js
rather thanwebpack --config webpack.config.js
. It bundled webpack itself and it took forever to bundle.I came across the same error. I am starting off with JS (with vuejs) and I would like to understand what this does?
I got this error when using the “fetch” library from npm. At first I got the same error but with other packages (
net
anddns
). My first thought was that those are missing dependencies. After runningnpm install net
andnpm install dns
, those errors were replaced withdgram
andfs
. The latter one prompted me to hit Google and landed on this issue.I now added two sections in my webpack config for both
fs
anddgram
, and now the errors are gone.But I don’t know what this entails. Will my compiled code still work? Even if these sections are configured as being
empty
? What does it actually do?Also, is this the right place to ask this? Or should it be rather targeted at vuejs?
nb: I would like to give a bit more insight about my running environment but as I’m still getting my feet wet, I don’t know which commands are the best to represent a snapshot of my dev-environment. If you need some additional output, let me know which commands to run.
So is there proper fix for this? I’m not having any luck in React with these workarounds.
The solution for me was to hack the Angular-CLI module to spoof the missing node modules.
After installing locate the following file:
Find the
node
line and add this:And that’s it!!!
🎉🎉🎉🎉🎉
Note: You will need to do this patch every time you update the package. So use this script:
package.json
patch-webpack.js
Source: https://blog.lysender.com/2018/07/angular-6-cannot-resolve-crypto-fs-net-path-stream-when-building-angular/
There is a better solution available hee
I have a React SSR (server side rendering) website, and both my server.js and browser.js go through webpack. For this purpose I have:
webpack.server.js:
webpack.browser.js:
This way my server.js can actually fs.write, and the browser only gets rid of compile errors.
I have a working solution for webpack to transfer any variables in the front-end, if it can help someone.
In your
webpack.config.client.js
before anything else:In your
webpack.config.client.js
in the plugins section just redefine anything you want in the front-end:And that’s all, now you can access
process.env.MY_VAR
from anywhere in the front-end and you don’t need to call the dotenv in your front-end.If any of you is using NextJS to fix this problem, you can add:
To your next.config.js file
The work-around allows us to run the webpack, but if our application needs to use FS for any reason, like write to file, the work-around is now a problem as FS no longer exists.
Does anyone have a different solution?
I’m having the same issue, I don’t know where I have to write the
@exhuma See the related page on documentation. That describes the default behavior. You can also find possible overrides there. I hope this helps.
Stack Overflow might be better for this type of questions.
Got this error.
on Doing
@executives what if you want to access the
fs
library outside ofwebpack.config.js
?Example: File:
configLoader.js
File:
webpack.config.js
File:
local/config.json
File:
production/config.json
I try and run
node dist/bundle.js
but I get the following error…I works if I run
But if I run the webpack command:
It fails…
Anyone know what’s going on?
None of these suggestions have worked for my env. How is this not fixed?
@majidgh72 Your solution lets webpack bundle the code successfully but when I run the app I get error
Uncaught reference Error: process is not defined
Could someone respond with an explicit file path (ex. node_modules/webpack/package.json) for where I need to add node: { fs: ‘empty’ }, and also include the entire file? I can’t find this magical webpack config file everyone is raving about. I have been struggling for a number of hours trying to get file-system to work and have tried adding node: { fs: ‘empty’ } to a number of places but it doesn’t work.
I’m working in angular 8.0.1
Also, if anyone knows a working alternative to file-system, let me know.
Thanks
mee too
Hi - when I did
target: 'node'
,the error went away. Then I got
global not defined
,so I fixed it with
'global': {},
inwebpack.definePlugin({...})
Then I got a GetEnv error saying SERVER_HOST not defined… but it is… so there’s a slew of errors that I’m swimming in - I hope someone can take a look at my boilerplate and give me hand? Thank you!
Modify
webpack.config.js
:Refer here
This link may help you.
@justinstander Here’s my settled-upon solution:
I keep a file one directory above my project that contains my relevant env vars in JSON format.
In my webpack.prod.config.js file, I perform the following code:
In my code, I use these vars like so:
This works for me in my environment. My sensitive data is kept in a file outside the project. Node gets to use
fs
to import that data, and WebPack gets to supply it in global vars. Those vars are not exposed on the window object.I hope this helps you with your project!
@J-Gallo Thank you so much!
next.config.js
I just had to add the
module: "empty",
as well.I got it to work when I added the following element inside
module.exports
in my webpack.config fileWhy is this issue closed? I just spent the last two days racking my brains, until I finally stumbled upon this bug report with a lucky combination of search keywords. The workaround seems like a really bad one. Will we see a fix?
@samayo I fixed the issue adding it to the
karma.conf.js
file. The file’s content is now like this:As you can see, at the end of the file, I added a new
webpack
entry and inside it the mentioned solution.For Webpack 4, you need to add this in
webpack.config.js
:For Webpack 5, you need to add this in
webpack.config.js
(upgrade to v5):So, this is for webpack 4 users. Maybe it might help others who bump into this thread. There is a configuration param called
target
. So this resolves the ‘fs’ error in webpackWhen i use fs as empty , there is no way for the bundled code to know /access fs modules of nodejs and that becomes a problem when we use it elsewhere in the code. So atleast I definitely do not recommend the fs:empty usage as suggested above
And here is my tsconfig. The module config is set to commonjs and target to es2015
{```
“compilerOptions”: { “module”: “commonjs”, “declaration”: true, “removeComments”: true, “emitDecoratorMetadata”: true, “experimentalDecorators”: true, “target”: “es2015”, “noErrorTruncation”: true, “allowJs”: true, “esModuleInterop”: true, “allowSyntheticDefaultImports”: true, “lib”: [“ES2015”, “ES2016”, “ES2018”, “ES2019”, “ES2020”, “ESNext”], “sourceMap”: true, “rootDir”: “./src”, “outDir”: “./dist”, “baseUrl”: “./”, “declarationMap”: true,
}, “include”: [“src/**/*.ts”], “exclude”: [“node_modules”, “dist”, “test”], “compileOnSave”: true }
For those of you using Nuxt.js for Vue.s server-side rendering, webpack can be extended as follows:
However, if you need
fs
for any reason, you may run into the same issues as mentioned above and require further workaroundsnpm install fs --save
doesn’t seem to have any affect in my case. https://webpack.js.org/configuration/target/ hints I should be usingtarget: 'web'
. This gives me Can’t resolve ‘fs’.node: { fs: "empty", }
gives me _fs2.default.readFileSync is not a function. Changing totarget: 'node'
gives me require is not defined (people also mentioning this in comments here.Oops, I’ve been trying to run a node module in the browser to do some validation. Of course, the browser doesn’t have file system access. In the end I just ran
build
on the node module and import or require the/dist
.js filepackage.json add
@Powersource Should be work without any problems on v5 https://github.com/webpack/webpack/blob/0c4d47a987f3688c663c7a7e18fe8e277e6f98c8/lib/node/NodeTargetPlugin.js#L27
@vijayakumar-psg587 so the fix for you was this
?
Came across this issue while playing with worker-loader, but the related issues in worker-loader are all marked as fixed. I just tried adding the
node: { fs: 'empty' }
trick, but also had to add module: ‘empty’ to it to get that to work.This seems to work, but has caused a four-fold increase in bundle size for the browser 😮 zero increase in the same module built for node.
wait for me to give a test repo
@bebraw With your fix of this issue, I have a
Uncaught ReferenceError: Buffer is not defined
in thelib/convert-source-map.js
file, in the browser.Thanks @majidgh72, the migration to webpack 5 talks about
fs
as a fallback.https://webpack.js.org/migrate/5/
Please provide minimum reproducible test repo
after adding {fs: ‘empty’} I get this:
external_"url":1 Uncaught ReferenceError: require is not defined at eval (external_"url":1) at Object.url (bundle.js:1065) at __webpack_require__ (bundle.js:683) at fn (bundle.js:60) at Object.eval (webpack:///(:8080/webpack)-dev-server/client?:6:11) at eval (webpack:///(:8080/webpack)-dev-server/client?:249:30) at Object../node_modules/webpack-dev-server/client/index.js?http://0.0.0.0:8080 (bundle.js:763) at __webpack_require__ (bundle.js:683) at fn (bundle.js:60) at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:1:1)
@samayo I’m not sure what I got could help you.
I fix this error by installing
node-sass
intodevDenpendency
, if I installnode-sass
intodenpendency
, I will get this error, I’m not sure you usenode-sass
or not, you can check some packages indenpendency
anddevDenpendency