Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Webpack config with "target: 'node'" and "type: 'module'" throws 'require is not defined'

When running my webpack config for an express server that uses ES6 modules, has "type": "module" defined in package.json and uses target: 'node' it fails with an an error ReferenceError: require is not defined.

When looking at the processed javascript file i see the following statements that cause this error:

/******/ (() => { // webpackBootstrap
/******/    "use strict";
/******/    var __webpack_modules__ = ({

/***/ "body-parser":
/*!******************************!*\
  !*** external "body-parser" ***!
  \******************************/
/***/ ((module) => {

module.exports = require("body-parser");

/***/ }),

Why does webpack include this code? The server runs totally fine when not processed with webpack.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved it by adding

experiments: {
  ...
  outputModule: true,
},
output: {
  ...
  chunkFormat: 'module', 
}

To my webpack.config.js

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!