Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack #1529

Closed
pho3nixf1re opened this issue Apr 6, 2015 · 41 comments
Closed

Webpack #1529

pho3nixf1re opened this issue Apr 6, 2015 · 41 comments

Comments

@pho3nixf1re
Copy link

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?

@jaredmcdonald
Copy link

+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

@pho3nixf1re
Copy link
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.

@jaredmcdonald
Copy link

Yeah, found the same, thanks. For anyone else that stumbles across this, the relevant webpack docs are here: http://webpack.github.io/docs/configuration.html#node

@vvo
Copy link
Contributor

vvo commented Apr 14, 2015

@jaredmcdonald thanks a lot :)

@simov
Copy link
Member

simov commented Apr 24, 2015

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

@pho3nixf1re
Copy link
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.

@varatep
Copy link

varatep commented Apr 27, 2015

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

@screendriver
Copy link

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'
  }
};

pho3nixf1re added a commit to pho3nixf1re/request that referenced this issue Jun 4, 2015
This adds a configuration for Webpack as a reference to anyone using it
over Browserify since it requires some special configuration to work.
There is a TODO task here to make the tests run with both Webpack and
Browserify configs in series, but that will take some work as the
current test setup won't allow that easily. For now, having this example
will help others to figure out how to get request working with Webpack.

This closes request#1529
pho3nixf1re added a commit to pho3nixf1re/request that referenced this issue Jun 4, 2015
This adds a configuration for Webpack as a reference to anyone using it
over Browserify since it requires some special configuration to work.
There is a TODO task here to make the tests run with both Webpack and
Browserify configs in series, but that will take some work as the
current test setup won't allow that easily. For now, having this example
will help others to figure out how to get request working with Webpack.

This closes request#1529
@kilometers
Copy link

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

@kilometers
Copy link

I just figured it out:

node: {
    console: true
}

pho3nixf1re added a commit to pho3nixf1re/request that referenced this issue Jul 14, 2015
This adds a configuration for Webpack as a reference to anyone using it
over Browserify since it requires some special configuration to work.
There is a TODO task here to make the tests run with both Webpack and
Browserify configs in series, but that will take some work as the
current test setup won't allow that easily. For now, having this example
will help others to figure out how to get request working with Webpack.

This closes request#1529
pho3nixf1re added a commit to pho3nixf1re/request that referenced this issue Jul 14, 2015
This adds a configuration for Webpack as a reference to anyone using it
over Browserify since it requires some special configuration to work.
There is a TODO task here to make the tests run with both Webpack and
Browserify configs in series, but that will take some work as the
current test setup won't allow that easily. For now, having this example
will help others to figure out how to get request working with Webpack.

This closes request#1529
@mattdell
Copy link

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

@nathanmarks
Copy link

Thanks all, bumped into this myself.

@mmahalwy
Copy link

mmahalwy commented Oct 7, 2015

any luck?

uttrasey pushed a commit to uttrasey/wt-namegame that referenced this issue Oct 11, 2015
@shirbr510
Copy link

I'm using webpack & request and got the error you all got here.
Tried the solution with the json-loader & node element and still getting the errors...

@derickablong
Copy link

Hi. I need help. I want to combine node js mysql into my react app. But i got error message cannot resolve module 'net' when i run the webpack.
Please see codes below:

import React from 'react'
import ReactDOM from 'react-dom'
import mysql from 'mysql'

export default class Server extends React.Component{
render(){
return(
<div>
<div>Success Inserting New Data</div>
<div>Company Name: {this.props.name}</div>
<div>Email: {this.props.email}</div>
</div>
);
}
}

var company_data = {
name: 'Company Name Sample',
email: 'email@gmail.com'
};

let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'earnmoredoless'
});

connection.connect();

let sql = connection.query('insert into account set ?', data, function(err, res){

`//success`
`if(!err){`

    `ReactDOM.render(`
        `<Server data={company_data} />,`
        `document.querySelector('#app')`
    `);`

`}else{`

    `console.log('Failed inserting new data.');`
`}`

});

@sohibul
Copy link

sohibul commented Feb 3, 2016

+1
this saved me

@carlospliego
Copy link

+1
Saved!

@crisu83
Copy link

crisu83 commented Mar 1, 2016

+1 I had problems with dotenv myself. The solution above solved my issue. Thanks!

@tiffanywei
Copy link

Adding node { fs: 'empty' } resolved the cannot resolve module 'fs' issue for me, but now my console has an error TypeError: fs.readFileSync is not a function. I'm running into this issue while trying to use dotev and webpack with es6 babel transpiler. Importing webpack as import 'dotenv/config'; as described in motdotla/dotenv#114

@richburdon
Copy link

While the node {} solution above helped my webpack.config.js it broke my karma.conf.js which depends on it, with the following error:

ReferenceError: Strict mode forbids implicit creation of global property '_crypto'

@ghost
Copy link

ghost commented Feb 15, 2017

Best easy solution without affecting bundled code:

require

@tuanh118
Copy link

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;
};

@chandrasekarb
Copy link

  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.

@lk22
Copy link

lk22 commented Aug 3, 2017

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

@FDiskas
Copy link

FDiskas commented Oct 18, 2017

target: 'node', worked for me 🏬

@wsrast
Copy link

wsrast commented Nov 6, 2017

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.

pokotyan added a commit to pokotyan/spotify that referenced this issue May 30, 2018
request-promiseを使うためにrequest、request-promise、json-loaderのインストールとwebpackの設定が必要だった。
request/request#1529
この設定しないとbuildした際にモジュールが見つからないって怒られる。
@stale
Copy link

stale bot commented Nov 23, 2018

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.

@reconbot reconbot closed this as completed Apr 1, 2019
@reconbot
Copy link
Contributor

reconbot commented Apr 1, 2019

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

@jonalxh
Copy link

jonalxh commented Jun 5, 2020

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests