‘require is not defined’ error when I try to run bundle.mjs created by webpack

I develop an app with node.js Fastify framework (typescript, ES syntax). I use Webpack to bundle the app and to compile typescript. The app bundles successfully, but when I try to run the resulting bundle.cjs file, I get the following error:

eferenceError: require is not defined in ES module scope, you can use import instead
    at Object.events (file:///home/myzader/vc-app/backend/dist/bundle.mjs:601068:1)
    at __webpack_require__ (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613956:41)
    at ./node_modules/pino/lib/proto.js (file:///home/myzader/vc-app/backend/dist/bundle.mjs:612505:26)
    at __webpack_require__ (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613956:41)
    at ./node_modules/pino/pino.js (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613549:15)
    at __webpack_require__ (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613956:41)
    at ./node_modules/fastify/lib/logger.js (file:///home/myzader/vc-app/backend/dist/bundle.mjs:607600:14)
    at __webpack_require__ (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613956:41)
    at ./node_modules/fastify/lib/reply.js (file:///home/myzader/vc-app/backend/dist/bundle.mjs:608101:21)
    at __webpack_require__ (file:///home/myzader/vc-app/backend/dist/bundle.mjs:613956:41)

I tried to adjust the webpack configs. When I add some package to the externals array, it dooesn’t throw the error any more, but I geet the same error for another package from dependencies. I need a help definitely, Thank you!

const config = {
  mode: 'development',
  devtool: 'source-map',
  entry: './app.ts',
  target: 'node',
  resolve: {
    extensions: ['.ts', '.js', '.mjs'],
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.mjs', // Change the extension to .mjs
    library: {
      type: 'module' // Change the library type to 'module'
    },
    chunkFormat: 'module' // Change the chunk format to 'module'
  },
  experiments: {
    outputModule: true
  },
  externals: [
    nodeExternals({
      allowlist: ['canvas', 'fastify', 'avvio', 'fastq', 'reusify', '@fastify/error', 'process-warning', 'abstract-logging', 'pino', 'pino-std-serializers', 'fast-redact', 'quick-format-unescaped'] 
    })
  ],
  module: {
    rules: [{
        test: /.(ts|js)$/, // Handle both .ts and .js files
        use: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /.node$/, // Apply this rule to files ending in .node
        loader: 'node-loader', // Use the node-loader for all .node files
      },
    ],
  },
  stats: {
    errorDetails: true, // Display details of errors
    colors: true, // Output in colored format, useful in console
    modules: true, // Display modules information
    reasons: true, // Display the reason about why modules are included
    warnings: true, // Display warnings
  },
  plugins: [
    new webpack.ContextReplacementPlugin(
      /awilix/, // The module that includes dynamic requires
      (data) => {
        delete data.dependencies[0].critical; // Remove the critical flag from these dependencies
        return data;
      }
    ),
  ],
};

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật