Nestjs p-throttle deployment
reblixt
PROOP

a year ago

Description

A common "Works on my machine" issue

I can build and run it locally on my PC.

This is a Micro service nest.js project with multiple apps.

I have deployed them separately and it builds with no issue.

However when it tries to deploy it causes an ESM issue,

I have tried to use dynamic import, downgraded p-throttle to a version where it had the CJS (version 4) but that did not fix it.

I have even tried to use "const pThrottle = require("p-throttle").default;"

But i still get the same issue.

According to my "AI" buddy it pointing towards Railways default config for webpack. But i have no clue if it is hallucinating.

I have checked my nest-cli.json and i have webpack = true in the compilerOptions and it does not get overwritten in the app it runs.

Code -- (nest.js) project

```

import pThrottle from 'p-throttle';

@Injectable()

export class SomeService {

private throttle = pThrottle({

limit: RATE_LIMIT_PER_SECOND,

interval: 1000,

});

}

```

Local machine.

- Linux Arch,

- Node version v22.14.0

A kollege of mine have WSL (Ubuntu) with the same node version and it runs on his PC aswell.

Logs from deployment

```

/app/dist/apps/object-processor/main.js:315

/* 8 */

^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/.pnpm/p-throttle@7.0.0/node_modules/p-throttle/index.js from /app/dist/apps/object-processor/main.js not supported.

Instead change the require of index.js in /app/dist/apps/object-processor/main.js to a dynamic import() which is available in all CommonJS modules.

at TracingChannel.traceSync (node:diagnostics_channel:315:14)

at Object.__decorate (/app/dist/apps/object-processor/main.js:395:18)

at __webpack_require__ (/app/dist/apps/object-processor/main.js:1601:42)

at Object. (/app/dist/apps/object-processor/main.js:183:22) {

code: 'ERR_REQUIRE_ESM'

}

Node.js v22.11.0

```

Solved$20 Bounty

Pinned Solution

mjablonski
PRO

a year ago

Hi,

you can run Railpack locally, so it might be easier to track down the root cause of the problem:

https://railpack.com/getting-started

Do you have set

"type": "module"

in your package.json, so Node defaults to ESM?

Another option would be to provide a dedicated Dockerfile in your project which gives you a controllable environment and usually uses less resources than the builds provided by Railpack / Nixpack.

A quick start could look something like this:

FROM node:lts-alpine

USER node
WORKDIR /home/node

COPY --chown=node:node package.json package-lock.json ./
RUN npm install && npm cache clean --force
COPY --chown=node:node . ./

RUN npm run build
CMD ["npm","start"]

2 Replies

mjablonski
PRO

a year ago

Hi,

you can run Railpack locally, so it might be easier to track down the root cause of the problem:

https://railpack.com/getting-started

Do you have set

"type": "module"

in your package.json, so Node defaults to ESM?

Another option would be to provide a dedicated Dockerfile in your project which gives you a controllable environment and usually uses less resources than the builds provided by Railpack / Nixpack.

A quick start could look something like this:

FROM node:lts-alpine

USER node
WORKDIR /home/node

COPY --chown=node:node package.json package-lock.json ./
RUN npm install && npm cache clean --force
COPY --chown=node:node . ./

RUN npm run build
CMD ["npm","start"]

reblixt
PROOP

a year ago

Hey mjablonski,

Thank you for your advice!

I did not set "type": "module".

But i went with the docker route, First i had to learn on how to create a fully functionall dockerfile, which was interersting! 🙂

But now it runs! 🙂


Status changed to Solved brody about 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...