Installing nodejs 10 + npm on alpine linux

Viewed 9291

I am using Alpine to build my Rails app and I am having some issues with some of its dependencies.

Right now, here's my Dockerfile:

FROM ruby:2.5.1-alpine
ENV BUNDLER_VERSION=2.0.2

RUN apk add --update --no-cache \
        binutils-gold \
        build-base \
        curl \
        file \
        g++ \
        gcc \
        git \
        less \
        libstdc++ \
        libffi-dev \
        libc-dev \ 
        linux-headers \
        libxml2-dev \
        libxslt-dev \
        libgcrypt-dev \
        make \
        netcat-openbsd \
        nodejs \
        openssl \
        pkgconfig \
        postgresql-dev \
        python \
        tzdata \
        yarn 

RUN gem install bundler -v 2.0.2
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install

COPY . /myapp
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 80

CMD ["rails", "server", "-b", "0.0.0.0", "-p", "80"]

My entrypoint.sh file looks like this:

#!/bin/sh
bundle install
rake db:migrate
rake db:seed
npm install
set -e

rm -f /myapp/tmp/pids/server.pid

exec "$@"

When it comes to npm install, I have issues trying to install puppeteer@3.0.0 (can't use 3.1.0 because of another error).

Here's the error when installing 3.1.0:

/myapp # npm install puppeteer@3.1.0

> puppeteer@3.1.0 install /myapp/node_modules/puppeteer
> node install.js

/myapp/node_modules/puppeteer/install.js:175
            } catch {
                    ^

SyntaxError: Unexpected token {
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puppeteer@3.1.0 install: `node install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the puppeteer@3.1.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-05-26T01_19_13_984Z-debug.log

Based on the lack of success getting 3.1.0 installed here, I was informed to try 3.0.0

So when trying to install 3.0.0, the following happens:

/myapp # npm install puppeteer@3.0.0

> puppeteer@3.0.0 install /myapp/node_modules/puppeteer
> node install.js

(node:111) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): The "original" argument must be of type function
(node:111) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
+ puppeteer@3.0.0
added 4 packages in 1.264s

According to this, the node version is old. Currently, I am running nodejs 8.9.3, as shown here:

/myapp # node -v
v8.9.3

I tried to install nodejs-current, but it seems like it uninstalled npm.

/myapp # apk add nodejs-current
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
(1/3) Purging nodejs-npm (8.9.3-r1)
(2/3) Purging nodejs (8.9.3-r1)
(3/3) Installing nodejs-current (9.2.1-r1)
Executing busybox-1.27.2-r11.trigger
OK: 300 MiB in 87 packages
/myapp # node -v
v9.2.1
/myapp # npm install puppeteer@3.0.0
/bin/sh: npm: not found

How can I upgrade my nodejs version without uninstalling npm? If I try to install npm again, the following error occurs:

/myapp # apk add npm
ERROR: unsatisfiable constraints:
  npm (missing):
    required by: world[npm]
/myapp # apk add --update npm
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  npm (missing):
    required by: world[npm]
2 Answers

Specify the required version for node like you did with ruby, FROM ruby:2.5.1-alpine , in the Dockerfile or docker-compose.yml file otherwise accordingly.
Check this link for the suitable version.

You need to use the update version of ruby which has the upgraded version of alpine kernel. For an e.g., you can use ruby:2.5.3-alpine3.9

FROM ruby:2.5.3-alpine3.9
ENV BUNDLER_VERSION=2.0.2

RUN apk add --update --no-cache \
        binutils-gold \
        build-base \
        curl \
        file \
        g++ \
        gcc \
        git \
        less \
        libstdc++ \
        libffi-dev \
        libc-dev \ 
        linux-headers \
        libxml2-dev \
        libxslt-dev \
        libgcrypt-dev \
        make \
        netcat-openbsd \
        nodejs \
        npm \
        openssl \
        pkgconfig \
        postgresql-dev \
        python \
        tzdata \
        yarn 

RUN gem install bundler -v 2.0.2
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install

and output of few commands

/ # npm -v
6.4.1
/ # node -v
v10.14.2
/ # cat /etc/issue
Welcome to Alpine Linux 3.9
Kernel \r on an \m (\l)

Related