NodeJS aws节点扩展/节点gyp上的停靠程序失败

xpcnnkqh  于 2022-11-03  发布在  Node.js
关注(0)|答案(1)|浏览(193)

几天来我一直在尝试让一个简单的dockerized应用程序在AWS上运行,它在本地运行得很好,但是在AWS EC2、Amazon Linux AMD上,我得到了这个错误:它找不到node_expat的绑定。docker构建在我的Ubuntu 22.04 docker桌面上运行得很好,运行得很好,但是在AWS服务器上的Ubuntu 22.04上,我在尝试运行container时遇到了这个错误--构建没有错误!我已经尝试了所有我能找到的东西,希望其他人也遇到过这个问题。我甚至尝试了节点14、16和18,都有同样问题

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'
   ]
 }
sczxawaw

sczxawaw1#

当然,经过几天的废话,我想明白了。为任何其他不幸的人发帖。我的Docker文件看起来基本上像


# 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" ]

这里的关键是将WORKDIR保留到最后。Docker仍然会缓存WORKDIR,即使你在构建过程中指定不缓存,太神奇了!另外,npm rebuild node-expat也是克服上述错误的关键。这在一些系统上是个怪癖。

相关问题