css-loader icon indicating copy to clipboard operation
css-loader copied to clipboard

Can't resolve 'fs' when bundle with webpack

Open Alex-Sokolov opened this issue 9 years ago • 100 comments

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.

  1. Update css-loader from 0.26.4 to 0.27.0 version
  2. 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

Alex-Sokolov avatar Mar 10 '17 06:03 Alex-Sokolov

same here

zsspub avatar Mar 10 '17 06:03 zsspub

same here

jano42 avatar Mar 10 '17 06:03 jano42

Same for me, had to rollback that update

Kamilius avatar Mar 10 '17 07:03 Kamilius

same here

gwuhaolin avatar Mar 10 '17 07:03 gwuhaolin

Adding

node: {
  fs: 'empty'
}

to webpack config file and the error is gone. Not sure how it works, It seems like webpack did some thing with fs module in web target.

hdnha11 avatar Mar 10 '17 07:03 hdnha11

add the following to your Webpack config:

node: {
   fs: "empty"
}

mehdivk avatar Mar 10 '17 07:03 mehdivk

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. 👍

bebraw avatar Mar 10 '17 07:03 bebraw

Did you can to unpublish this version? It will keep calm to all of us

PredokMiF avatar Mar 10 '17 07:03 PredokMiF

@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

node: {
  fs: 'empty'
}

If using this workaround isn't viable, version lock to the previous minor version.

joshwiens avatar Mar 10 '17 08:03 joshwiens

@bebraw With your fix of this issue, I have a Uncaught ReferenceError: Buffer is not defined in the lib/convert-source-map.js file, in the browser.

fklingler avatar Mar 13 '17 11:03 fklingler

@fklingler I have a proposed fix for that at https://github.com/webpack-contrib/css-loader/pull/453 . Still waiting for some approvals before I can push it. Feel free to give that a go and let me know how it goes.

bebraw avatar Mar 13 '17 12:03 bebraw

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 and dns). My first thought was that those are missing dependencies. After running npm install net and npm install dns, those errors were replaced with dgram and fs. 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 and dgram, 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.

exhuma avatar Jun 05 '17 09:06 exhuma

@exhuma See the related page on documentation. That describes the default behavior. You can also find possible overrides there. I hope this helps.

Also, is this the right place to ask this? Or should it be rather targeted at vuejs?

Stack Overflow might be better for this type of questions.

bebraw avatar Jun 05 '17 10:06 bebraw

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

The target: 'node' option tells webpack not to touch any built-in modules like fs or path.

stephenhmarsh avatar Jun 16 '17 02:06 stephenhmarsh

Hi - when I did target: 'node',

the error went away. Then I got global not defined,

so I fixed it with 'global': {}, in webpack.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!

adambarghouthi avatar Jul 19 '17 02:07 adambarghouthi

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

jollyblume avatar Jul 29 '17 01:07 jollyblume

Someone please fix this issue. Non of the mentioned answer could fix it for me

samayo avatar Aug 30 '17 13:08 samayo

@samayo I'm not sure what I got could help you.

I fix this error by installing node-sass into devDenpendency, if I install node-sass into denpendency, I will get this error, I'm not sure you use node-sass or not, you can check some packages in denpendency and devDenpendency

dawncold avatar Sep 06 '17 09:09 dawncold

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, in webpack.config.js of module A, I add:

  node: {
    console: false,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }

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)?

engelen avatar Oct 02 '17 19:10 engelen

Same problem - why is this issue closed? Should I 👍 a different issue?

leegee avatar Nov 14 '17 10:11 leegee