Webpack, no function required

Getting an error when starting the React application for the embedded webpack - the error is as follows:

webpack-internal:///1495:3
var crypto = require('crypto');
             ^

TypeError: require is not a function
    at eval (webpack-internal:///1495:3:14)
    at Object.<anonymous> (/path/to/project/build/main.js:9739:1)
    at __webpack_require__ (/path/to/project/build/main.js:21:30)
    at eval (webpack-internal:///1494:1:20)
    at Object.<anonymous> (/path/to/project/build/main.js:9733:1)
    at __webpack_require__ (/path/to/project/build/main.js:21:30)
    at eval (webpack-internal:///692:8:18)
    at Object.<anonymous> (/path/to/project/build/main.js:4515:1)
    at __webpack_require__ (/path/to/project/build/main.js:21:30)
    at eval (webpack-internal:///1491:12:23)

I can’t understand which module this error comes from. I start my project with this command:

NODE_ENV=development nodemon --watch build/ build/main.js

Here is my webpack configurator:

const path = require('path')
const fs = require('fs')
const webpack = require('webpack')

const webpackConfig = {
  context: path.join(__dirname, '..'),
  entry: ['babel-polyfill', path.join(__dirname, '../src/entry.js')],
  target: 'node',
  output: {
    path: path.resolve(__dirname, '../build'),
    publicPath: '/',
    libraryTarget: 'commonjs2',
    filename: 'main.js'
  },
  resolve: {
    modules: [
      path.join(__dirname, '../src'),
      path.join(__dirname, '../server'),
      'node_modules'
    ],
    extensions: ['.js', '.jsx', '.json']
  },
 module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.(png|gif|jpe?g|ico|eot|svg|ttf|woff2?)$/,
        loader: 'file-loader',
        options: {
          context: 'src/app/assets/',
          outputPath: 'dist/',
          name: '[path][name].[ext]?[hash]',
          //limit: 10000
        }
      }
    ]
  },
  node: {
    net: 'empty',
    tls: 'empty',
    dns: 'empty',
    fs: 'empty',
    mysql: 'empty',
    __dirname: true
  },
  plugins: [
    // hot reload new webpack.HotModuleReplacementPlugin(),
    new webpack.IgnorePlugin(/webpack-stats\.json$/),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('development'),
        API_HOST: JSON.stringify(process.env.API_HOST),
        API_PORT: JSON.stringify(process.env.API_PORT)
      },
    }),

  ],
  externals: ['mysql', 'bindings']
}

if (process.env.NODE_ENV ===  'development') {
  webpackConfig.devtool = 'eval-source-map'
}

module.exports = webpackConfig

The file is created using this command:

./node_modules/.bin/webpack --display-error-details --config webpack/webpack.config.js

The version of webpack used is 3.0.0.

+4
source share
1 answer

If you still have requirein your package, this means that some ES6 files were not transferred to Babel.

I see that you are excluding node_modulesfrom babel-loader...

Can you try to remove the exception and see if it is better?

, "require (" crypto ") arborescence?

, , React, babel-loader .js, .jsx... test /\.jsx?$/?

0

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


All Articles