Skip to content

Webpack #1529

Closed
Closed

Description

@pho3nixf1re

I'm attempting to load request using Webpack and am seeing this error:

screen shot 2015-04-06 at 13 04 59

I have seen that this is supposed to work in the browser using browserify and was curious what I'm doing wrong. Maybe browserify processes the dependencies differently from webpack which is causing this issue. Any ideas?

Activity

  1. jaredmcdonald commented on Apr 6, 2015

    @jaredmcdonald

    +1, also seeing this

    Output:

    ERROR in ./~/request/lib/har.js
    Module not found: Error: Cannot resolve module 'fs' in /absolute/path/to/project/node_modules/request/lib
     @ ./~/request/lib/har.js 3:9-22
    
    ERROR in ./~/request/~/tunnel-agent/index.js
    Module not found: Error: Cannot resolve module 'net' in /absolute/path/to/project/node_modules/request/node_modules/tunnel-agent
     @ ./~/request/~/tunnel-agent/index.js 3:10-24
    
    ERROR in ./~/request/~/tunnel-agent/index.js
    Module not found: Error: Cannot resolve module 'tls' in /absolute/path/to/project/node_modules/request/node_modules/tunnel-agent
     @ ./~/request/~/tunnel-agent/index.js 4:10-24
    
    ERROR in ./~/request/~/forever-agent/index.js
    Module not found: Error: Cannot resolve module 'net' in /absolute/path/to/project/node_modules/request/node_modules/forever-agent
     @ ./~/request/~/forever-agent/index.js 6:10-24
    
    ERROR in ./~/request/~/forever-agent/index.js
    Module not found: Error: Cannot resolve module 'tls' in /absolute/path/to/project/node_modules/request/node_modules/forever-agent
     @ ./~/request/~/forever-agent/index.js 7:10-24
    
    ERROR in ./~/request/~/tough-cookie/lib/cookie.js
    Module not found: Error: Cannot resolve module 'net' in /absolute/path/to/project/node_modules/request/node_modules/tough-cookie/lib
     @ ./~/request/~/tough-cookie/lib/cookie.js 23:10-24
    
    ERROR in ./~/request/~/form-data/lib/form_data.js
    Module not found: Error: Cannot resolve module 'fs' in /absolute/path/to/project/node_modules/request/node_modules/form-data/lib
     @ ./~/request/~/form-data/lib/form_data.js 7:9-22
    
    ERROR in ./~/request/~/har-validator/~/is-my-json-valid/~/jsonpointer/jsonpointer.js
    Module not found: Error: Cannot resolve module 'console' in /absolute/path/to/project/node_modules/request/node_modules/har-validator/node_modules/is-my-json-valid/node_modules/jsonpointer
     @ ./~/request/~/har-validator/~/is-my-json-valid/~/jsonpointer/jsonpointer.js 1:14-32
    
  2. pho3nixf1re commented on Apr 6, 2015

    @pho3nixf1re
    Author

    I figured it out. The problem is that Webpack requires some extra config unlike browserify due to it's more barebones nature. See my example branch and diff for how I got the tests to run successfully.

    master...pho3nixf1re:webpack-tests

    My plan is to turn that into it's own karma test and submit a PR so others can use it as an example. Also, it may uncover future issues as development progresses.

  3. jaredmcdonald commented on Apr 7, 2015

    @jaredmcdonald
  4. vvo commented on Apr 14, 2015

    @vvo
    Contributor

    @jaredmcdonald thanks a lot :)

  5. simov commented on Apr 24, 2015

    @simov
    Member

    @pho3nixf1re it would be great if you submit a PR about testing request with webpack (additional test, not in place of the browserify one) 👍

  6. pho3nixf1re commented on Apr 24, 2015

    @pho3nixf1re
    Author

    Yes! I intend to. I only replaced it in my branch as a quick proof of concept. I'll get on that this weekend.

  7. varatep commented on Apr 27, 2015

    @varatep

    Adding the json webpack loader unfortunately did not fix this for me.

  8. screendriver commented on May 19, 2015

    @screendriver

    Same issue here.

    The solution: install json-loader so that webpack can handle *.json files. Then add that loader and a new node object to your webpack.config.js like so:

    $ npm install --save-dev json-loader

    webpack.config.js

    var path = require('path');
    
    module.exports = {
      entry: 'index',
      output: {
        path: path.join(__dirname, 'scripts'),
        filename: 'bundle.js'
      },
      module: {
        loaders: [
          { test: /\.json$/, loader: 'json-loader' }
        ]
      },
      resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.js']
      },
      node: {
        console: 'empty',
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    };
  9. added 2 commits that reference this issue on Jun 4, 2015
    43b505c
    182c3af
  10. kilometers commented on Jun 14, 2015

    @kilometers

    Adding the node: { ... } object avoids the error, but then I can't use console in the browser. Is there a way around this?

  11. kilometers commented on Jun 14, 2015

    @kilometers

    I just figured it out:

    node: {
        console: true
    }
    
  12. added 2 commits that reference this issue on Jul 14, 2015
    38506ee
    49e3022
  13. mattdell commented on Jul 21, 2015

    @mattdell

    This was a huge help for me. Thanks to all above.

  14. 20 remaining items

  15. brickgale commented on Dec 13, 2016

    @brickgale

    +1
    Thanks!

  16. trentmillar commented on Jan 11, 2017

    @trentmillar

    I made a small change using what @screendriver & @kilometers provided (thanks!).

    Webpack complained if there was an empty extension, however, I don't think this would be required for most.

    var path = require('path')
    var request = require('request')
    var webpack = require('webpack')
    
    module.exports = {
      entry: './src/main.js',
      output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: '/dist/',
        filename: 'build.js'
      },
      module: {
        rules: [
          {
            test: /\.json$/,
            loader: 'json-loader'
          }
        ]
      },
      resolve: {
        /*extensions: ['.webpack.js', '.web.js', '.js']*/
      }
      node: {
        console: true,
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    }
    
  17. tuanh118 commented on Feb 21, 2017

    @tuanh118

    For peeps who have trouble with storybook and don't want to replace storybook's original webpack config, you can extend it by creating .storybook/webpack.config.js and add:

    // Load the default config generator.
    const genDefaultConfig = require('@kadira/storybook/dist/server/config/defaults/webpack.config.js');
    
    module.exports = function (config, env) {
        const newConfig = genDefaultConfig(config, env);
    
        // Extend to resolve fs issues
        newConfig.node = {
            fs: 'empty'
        };
    
        return newConfig;
    };
  18. added a commit that references this issue on Apr 19, 2017
  19. chandrasekarb commented on May 12, 2017

    @chandrasekarb
      node: {
        console: true,
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    
    

    Using this above code error has been solved but

    fs.writeFile('/home/react-hello-world/test.txt', 'aaa', function(err) { alert(err); });

    fs.writeFile is not working please give any solution.

  20. lk22 commented on Aug 3, 2017

    @lk22

    @chandrasekarb try set fs to true instead of 'empty'

  21. FDiskas commented on Oct 18, 2017

    @FDiskas

    target: 'node', worked for me 🏬

  22. wsrast commented on Nov 6, 2017

    @wsrast

    I ran into this issue and it was because I was running a dual client/server config for server side rendering. The server-side rendering config was marked as target: 'node', while the client config didn't have one specified. I added target: 'web' to the client config and the error went away.

  23. stale commented on Nov 23, 2018

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  24. reconbot commented on Apr 1, 2019

    @reconbot
    Contributor

    Request is now in maintenance mode and wont be merging any new features or addressing this bug. Please see #3142 for more information.

  25. jonalxh commented on Jun 5, 2020

    @jonalxh

    I had the same issue trying to use jQuery with Nuxt (or Node), the problem was I was importing jQuery and Bootstrap and added jsdom to solve it but did not work. I solved it importing libraries directly in head section of nuxt.config.js, another way is to set client mode in nuxt config plugins, I hope it help someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions