Chris Schaub
Chris Schaub

Reputation: 101

docker on aws node-expat / node-gyp fails

I've been trying for a few days to get a simple dockerized app with node-expat to run on aws. It works fine locally, but on AWS EC2, Amazon Linux AMD, I get this error: it can't find the binding for node_expat. The docker build works fine on my Ubuntu 22.04 docker desktop, runs fine, but on Ubuntu 22.04 on AWS server, I get this error trying to run container -- the build does not error! I've tried all of the stuff I can find, hoping somebody else has run into this. I've even tried node 14, 16 and 18, all have same issue.

exited with code 1
 /usr/src/nodepp/node_modules/bindings/bindings.js:135
   throw err;
   ^
 
 Error: Could not locate the bindings file. Tried:
  → /usr/src/nodepp/node_modules/node-expat/build/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/build/Debug/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/build/Release/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/out/Debug/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/Debug/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/out/Release/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/Release/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/build/default/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/compiled/14.20.1/linux/x64/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/addon-build/release/install-root/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/addon-build/debug/install-root/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/addon-build/default/install-root/node_expat.node
  → /usr/src/nodepp/node_modules/node-expat/lib/binding/node-v83-linux-x64/node_expat.node
     at bindings (/usr/src/nodepp/node_modules/bindings/bindings.js:126:9)
     at Object.<anonymous> (/usr/src/nodepp/node_modules/node-expat/lib/node-expat.js:4:34)
     at Module._compile (internal/modules/cjs/loader.js:1085:14)
     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
     at Module.load (internal/modules/cjs/loader.js:950:32)
     at Function.Module._load (internal/modules/cjs/loader.js:790:12)
     at Module.require (internal/modules/cjs/loader.js:974:19)
     at require (internal/modules/cjs/helpers.js:101:18)
     at Object.<anonymous> (/usr/src/nodepp/node_modules/xml2json/lib/xml2json.js:1:13)
     at Module._compile (internal/modules/cjs/loader.js:1085:14) {
   tries: [
     '/usr/src/nodepp/node_modules/node-expat/build/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/build/Debug/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/build/Release/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/out/Debug/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/Debug/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/out/Release/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/Release/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/build/default/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/compiled/14.20.1/linux/x64/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/addon-build/release/install-root/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/addon-build/debug/install-root/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/addon-build/default/install-root/node_expat.node',
     '/usr/src/nodepp/node_modules/node-expat/lib/binding/node-v83-linux-x64/node_expat.node'
   ]
 }

Upvotes: 0

Views: 412

Answers (1)

Chris Schaub
Chris Schaub

Reputation: 101

And of course, after days of nonsense, I figured it out. Posting for any other unlucky person. My docker file looks basically like

# Build container
FROM node:18-alpine

# Make directory for app
RUN mkdir -p /usr/src/app

# Install packages
COPY package.json ./
RUN apk add --no-cache --virtual python3 g++ make
RUN npm install

# Copy the packages and source to destination
COPY . ./usr/src/app

# Set working dir almost last to prevent caching
# Docker caches the work dir otherwise
WORKDIR /usr/src/app

# Fixes a really bad bug with node-expat and gyp
# where the container will not build on on some systems
# especially AWS EC2 amazon linux/amd
RUN ["npm", "rebuild", "node-expat" ]

The key thing here is to keep the WORKDIR almost to the end. Docker will still cache the WORKDIR, even if you specify no caching during build, amazing! Also, the npm rebuild node-expat is also key to get past the error a bove. A quirk on some systems.

Upvotes: 0

Related Questions