0

I want to bundle multiple TypeScript files and generate a file that bundles everything to be used through node build/bundle.js and that can be copy-pasted to CodinGame. Everything works (transpilation, building, linting with Biome and unit tests with Jest including execution) except calling the bundle through Node.js (npm run test, removed after the above extreme simplification, that simply calls jest, works perfectly).

I don't want a standalone executable (./bundle), because I want a callable one through Node.js (node build/bundle.js) and I guess that the same output will be fine for CodinGame.

Building works perfectly fine, but the output is invalid, at least for my purpose (or my version of Node.js, but JS should have changed). My operating system is Debian GNU/Linux 13 "Trixie" (the current stable) and I installed Node.js (v20.19.2) through APT (the package manager of Debian, Ubuntu and many more distros).

In production mode of Webpack, node build/bundle.js causes TypeError: r is not a function (and TypeError: e is not a function before my code extreme simplification). In development mode of Webpack, node build/bundle.js causes readline is not a function, even with externals: {readline: "require('readline')",}, in webpack.config.ts.

My src/main.ts is very simple:

const readline = require("node:readline");
console.log(readline());

Here is my tsconfig.json:

{
    "compilerOptions": {
        "rootDir": ".",
        "outDir": "./build",

        "module": "nodenext",
        "target": "esnext",
        "lib": ["esnext"],
        "types": ["node"],

        // Other Outputs
        "sourceMap": true,
        "declaration": true,
        "declarationMap": true,

        "strict": true,
        "isolatedModules": true,
        "noUncheckedSideEffectImports": true,
        "moduleDetection": "force",
        "skipLibCheck": true
    }
}

To generate a bundle I use Webpack:

const path = require("path");

module.exports = {
    entry: "./src/main.ts",
    output: {
        filename: "bundle.js", // Name of the bundled output file
        path: path.resolve(__dirname, "build"), // Output directory
        clean: true, // Clean the output directory before each build
    },
    resolve: {
        extensions: [".ts"], // Resolve these file extensions when importing
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/, // Apply this rule to .ts and .tsx files
                use: "ts-loader", // Use ts-loader to transpile TypeScript
                exclude: /node_modules/, // Exclude dependencies
            },
        ],
    },
    mode: "production",
    target: "node",
};

Finally, my package.json is the following:

{
    "name": "webpack-exe",
    "version": "1.0.0",
    "main": "main.ts",
    "scripts": {
        "build": "webpack"
    },
    "devDependencies": {
        "@types/node": "^24.10.1",
        "ts-loader": "^9.5.4",
        "ts-node": "^10.9.2",
        "typescript": "^5.9.3",
        "webpack": "^5.103.0",
        "webpack-cli": "^6.0.1"
    }
}

What am I missing to simply be able to do node build/bundle.js and be able to copy-paste it for CodinGame?

My current not-working generated build/bundle.js is:

(()=>{"use strict";var r={481:r=>{r.exports=require("node:readline")}},e={};function o(t){var s=e[t];if(void 0!==s)return s.exports;var n=e[t]={exports:{}};return r[t](n,n.exports,o),n.exports}(()=>{const r=o(481);console.log(r())})()})();

I understand why it does not work, but I don't understand why Webpack generates this and how to make it generate what I want.

TypeError: r is not a function
    at /home/me/workspace/webpack-hell/build/bundle.js:1:227
    at /home/me/workspace/webpack-hell/build/bundle.js:1:233
    at Object.<anonymous> (/home/me/workspace/webpack-hell/build/bundle.js:1:237)
    at Module._compile (node:internal/modules/cjs/loader:1529:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1613:10)
    at Module.load (node:internal/modules/cjs/loader:1275:32)
    at Module._load (node:internal/modules/cjs/loader:1096:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:164:12)
    at node:internal/main/run_main_module:28:49
New contributor
user31922018 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Try running your typescript directly using a tool like tsx - you'll find that readline is not a function. Commented 16 hours ago
  • You should also be seeing an error in your IDE Commented 15 hours ago
  • Total sidenote: modern Node has had testing built in for a while now so any new project you're adding jest to just... doesn't need it. See nodejs.org/api/test.html for more details. And if you need bundling, using esbuild over webpack is almost certainly a faster and "you can just specify everything on the command line, no need for a config. Which means you can also write it as an npm script and done". Commented 14 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.