MCP Express

How to Bundle Your Server-Side JavaScript With esbuild (Including Third-Party Packages)

Get Started

How to Bundle Your Server-Side JavaScript With esbuild (Including Third-Party Packages)

To run your custom JavaScript code inside our platform, you need to bundle your entire project — including all third-party packages — into a single .cjs file.

This means:

  • Your bundle must be CommonJS (require, not ESM-only).
  • Your bundle must target Node.js, not the browser.
  • You must explicitly export functions and call them yourself if needed.

This guide walks you through bundling everything correctly with esbuild.


✅ What You’ll Need

  • Node.js (16+)
  • npm or yarn
  • Your JavaScript or TypeScript project
  • A terminal

1. Install esbuild

npm install --save-dev esbuild

If using TypeScript:

npm install --save-dev typescript

2. Add Third-Party Dependencies

Install any libraries you need:

npm install lodash axios dayjs

Use them normally:

import _ from "lodash";
import axios from "axios";

function main() {
  console.log(_.capitalize("hello world"));
  return axios.get("https://api.example.com");
}
main()

esbuild will include everything automatically.


3. Create a Build Script (Node + CommonJS)

Create a file named build.js:

// build.js
const esbuild = require("esbuild");

esbuild.build({
  entryPoints: ["src/index.js"], // or index.ts
  bundle: true,
  platform: "node",      // Server-side execution
  format: "cjs",         // CommonJS output
  target: "node18",      // Adjust if needed
  outfile: "dist/bundled_code.cjs",
  minify: true,
}).catch(() => process.exit(1));
Why these settings?
Option Meaning
platform: "node" Optimizes output for Node.js execution
format: "cjs" Ensures compatibility with node provided-code.cjs
target: "node18" Uses Node.js builtin features
outfile Your final file that you’ll upload
bundle: true Includes all files + NPM packages

4. Add a Build Script to package.json

{
  "scripts": {
    "build": "node build.js"
  }
}

Build it with:

npm run build

5. Example Project Structure

my-project/
│
├── src/
│   ├── index.js
│   └── utils.js
│
├── node_modules/
├── build.js
├── package.json
└── dist/
    └── provided-code.cjs  <-- upload this file

6. Example: Bundling Server-Side Code

src/index.js
import dayjs from "dayjs";
import { greet } from "./utils";

export function main() {
  console.log(greet("Sachin"));
  console.log("Current time:", dayjs().format());
}
main()
src/utils.js
export function greet(name) {
  return `Hello, ${name}!`;
}

Build it:

npm run build

Your final upload file will be:

dist/bundled_code.cjs

7. How to Test Your Bundled Code Inside the Platform

After uploading provided_code.cjs, you can test and run your functions directly from our Test Tools interface.

▶️ Follow these steps:
  1. Open the Test Tools page in your dashboard.
  2. Select a client you want to use to connect to your MCP server.
  3. Once connected, choose the tool that contains the function(s) you exported in your bundled code
  4. .Provide any required input parameters.
  5. Click Run Tool to execute the function and instantly see the output.

This lets you verify:

  • Your bundled code runs correctly
  • All your dependencies were bundled properly
  • Your logic behaves as expected inside our server environment

8. Using TypeScript? (Optional)

Just change entry points:

entryPoints: ["src/index.ts"],

esbuild auto-transpiles TypeScript — no config needed.


9. Troubleshooting

❌ “Cannot find module”

Make sure the dependency is installed:

npm install <package>
❌ Bundle crashes on import

Use:

format: "cjs"
platform: "node"

❌ Functions don’t run

Add explicit calls in your script:

run();

🎉 Final Step: Upload & Execute

Upload:

dist/bundled_code.cjs

Your code will run exactly as written, using Node.js on our server.

Cart (0 items)