How to fix node-gyp build errors with Python 3.11

When upgrading to alpine 3.19, we found that we could no longer build the native bcrypt package because the npm rebuild command would fail with:

invalid mode: 'rU' while trying to load binding.gyp

This is caused by the antiquated Node14/npm 6 version that meteor uses and the final removal of the deprecated “U” flag in Python 3.11. Read more here.

To fix this, you can patch the input.py file in place, e.g. in your Dockerfile before building native packages:

ENV PATCHED_INPUT_PY_PATH="$NODE_PATH/npm/node_modules/node-gyp/gyp/pylib/gyp/input.py"

RUN sed -i "s/'rU'/'r'/g" "$PATCHED_INPUT_PY_PATH"

Hope this helps someone

/Per

Another approach would have been to update node-gyp itself, I guess…
v9.x of node-gyp (supports node 14) does not seem to have this problem with Python 3.11

@permb

Also, alternative to native bcrypt is bcryptjs.

We found bcryptjs to be too slow but YMMV