Using webpack to use the required modules in the browser

The last 2 days I tried to use require ('modules') in a browser with webpack, when I could do the same in browserify in 5 minutes ...

Here is 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, whatever I do, I get some kind of error. I am currently getting:

bundle.js:390 Uncaught Error: Cannot find module "net"

and when I start webpack, it throws these errors: http://pastebin.com/RgFN3uYm

I followed https://webpack.imtqy.com/docs/tutorials/getting-started/ and http://www.pauleveritt.org/articles/pylyglot/webpack/, but still getting these errors.

I tried to run it using this: webpack./main.js -o bundle.jsAnd yet it still doesn't work.

How can this be solved?

+6
3

, .

 resolve: {
        modulesDirectories: ['./app/', './node_modules']
 }

Update: json

npm install --save-dev json-loader

module: {
    loaders: [
      { test: /\.json$/, loader: 'json-loader' }
    ]
  }

fs, net, tls - node.js . :

node: {
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
+2

?

:

packages.json
node_modules/net/
webpack.config.js
src/main.js

main.js

var net = require('net');

webpack.config.js:

const path = require('path');

const PATHS = {
    src: path.join(__dirname, 'src'),
    dist: path.join(__dirname, 'dist')
};

module.exports = {
    entry: path.join(PATHS.src, 'main.js'),
    output: {
        path: PATHS.dist,
        filename: 'bundle.js'
    }
}

webpack, , index.html, , main.js!

0

.

, . . :

Node.js® - JavaScript, Chrome V8 JavaScript.

, JavaScript . , JavaScript, JavaScript, , , , , require(). Node, V8.

, , . c++, . , javascript , , , javascript.

.

:

const fs = require('fs');

, ? ? , , -, , .

, fs, tls net, , , (" ?") (", JavaScript "). tcp ") .

0

Source: https://habr.com/ru/post/1658626/


All Articles