Skip to content

Node build artefacts in Docker container fails to run because of source-map-support/register

I’m using Backpack to build my node app. When running the app locally, the app itself and the build artefacts are doing okay. But when I move the build results to a Docker image and try to run it, I get the following errors:

Error: Cannot find module 'source-map-support/register'
Require stack:
- /home/app/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/app/main.js:1:1)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/home/app/main.js' ]

My Dockerfile:

FROM node:12.21-alpine

ENV NODE_ENV=production

RUN mkdir -p /home/app

COPY ./build /home/app

WORKDIR /home

CMD ["node", "./app/main.js"]

What am I missing? Are there other alternatives for Backpack? Basically, I want to build my app first, move the artefacts into a Docker image and then run that. I’m not looking to build inside an image.

Answer

You are missing node_modules.
You have to copy node_modules to container COPY ./node_modules /home/app/node_modules
Or install it during the docker build (remember to copy package.json and package-lock.json) RUN npm ci.
If it is a local development env, you can choose the first option. But if not, a better way is second one.