webpack: TypeError: __webpack_require__(...) is not a function

Bug report

What is the current behavior?

TypeError: __webpack_require__(...) is not a function

If the current behavior is a bug, please provide the steps to reproduce.

it’s a really simple situation that takes you 5 minutes to check the entire codes.

// ./src/index.js
require('./other');


// ./src/other.js
module.exports = { a: 1 };


// ./babel.config.js
module.exports = {
    "presets": ["@babel/preset-env"],
    "plugins": ["babel-plugin-source-map-support"]
};


// ./webpack.config.js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpackNodeExternals = require('webpack-node-externals');

module.exports = {
    mode: 'development',
    target: 'node',
    devtool: 'source-map',
    entry: './src/index.js',
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [{ test: /\.js$/, use: 'babel-loader' }],
    },
    plugins: [
        new CleanWebpackPlugin(),
    ],
    externals: [webpackNodeExternals()],
};
// package.json
{
  "name": "webpack-bug",
  "version": "0.0.1",
  "description": "",
  "main": "./dist/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "UNLICENSED",
  "devDependencies": {
    "@babel/cli": "^7.5.0",
    "@babel/core": "^7.5.0",
    "@babel/preset-env": "^7.5.0",
    "babel-loader": "^8.0.6",
    "babel-plugin-source-map-support": "^2.0.1",
    "clean-webpack-plugin": "^3.0.0",
    "webpack": "^4.35.2",
    "webpack-cli": "^3.3.5",
    "webpack-node-externals": "^1.7.2"
  },
  "dependencies": {
    "source-map-support": "^0.5.12"
  }
}

you can clone the repo for checking.

$git clone git@github.com:zimtsui/webpack-bug.git
$cd ./webpack-bug
$npm i
$npm run build
$node .
TypeError: __webpack_require__(...) is not a function

babel-plugin-source-map-support is a simple babel plugin, which just add a single line at the beginning.

$npx babel ./src/index.js
"use strict";

require("source-map-support/register"); // the plugin adds this line

require('./other');

if you disable the plugin and manually add that line in source file, everything will be ok:

// ./babel.config.js
module.exports = {
    "presets": ["@babel/preset-env"],
    // "plugins": ["babel-plugin-source-map-support"]
};


// ./src/index.js
require('source-map-support/register');
require('./other');

if you use exports instead of module.exports to export, everything will be ok as well:

// ./src/other.js
exports.a = 1;
// module.exports = { a: 1 };

What is the expected behavior? to exit normally and output nothing.

Other relevant information: webpack version: 4.35.2 Node.js version: 10.16.0 lts Operating System: ubuntu 18.04 Additional tools:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

@zimtsui it is not webpack problem as we said above, also i can’t reproduce problem using your reproduce test repo

I could reproduce the problem with your repo.

Adding sourceType: "unambiguous" to babel.config.js fixes the problem.

webpack includes all build-in modules such as fs path

These are modules source-map-support/register are using, so that’s fine