Using webpack to use require modules in browser
browserify, express, javascript, node.js, webpack
Solution
You should add directories to resolve e.g.
resolve: {
modulesDirectories: ['./app/', './node_modules']
}
Update: Add json loader
npm install --save-dev json-loader
module: {
loaders: [
{ test: /\.json$/, loader: 'json-loader' }
]
}
also fs, net, tls are libraries for node.js not for in-browser usage. You should add:
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
}
Problem
Tried for the past 2 days to use require('modules') in the browser with webpack, when I could do the same thing in browserify in 5 minutes... Here's my webpack.config.js ``` var webpack = require('webpack'); var path = require('path'); var fs = require('fs'); var nodeModules = {}; fs.readdirSync('node_modules') .filter(function(x) { return ['.bin'].indexOf(x) === -1; }) .forEach(function(mod) { nodeModules[mod] = 'commonjs ' + mod; }); module.exports = { entry: "./main.js", output: { filename: "bundle.js" } } ``` However, no matter what I do I get some sort of error. Currently I am getting: ``` bundle.js:390 Uncaught Error: Cannot find module "net" ``` and when I run webpack it throws these errors: http://pastebin.com/RgFN3uYm I followed https://webpack.github.io/docs/tutorials/getting-started/ and http://www.pauleveritt.org/articles/pylyglot/webpack/ yet I still get these errors. I've tried to run it with this: `webpack ./main.js -o bundle.js` Yet it still doesn't work. How can this be resolved?