Search code examples
node.jsreactjsdeploymentgitlab-cicicd

Deploying React app on Debian 11 with serve throws unexplainable error in the CI/CD Pipeline on gitlab


The following as the pipeline we are using to deploy the App to the Debian server.

stages:   
  - deploy

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  environment: production
  image: node:latest  
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts


  script:
    - echo "Deploying application..."
    - ssh $SSH_USER@$SSH_IP "cd $PROEJCT_PATH/$PROJECT_DIRECTORY_NAME && serve -s build"
    - echo "Application successfully deployed."

But this throws the following Error Message:

file:///usr/local/lib/node_modules/serve/build/main.js:169
const ipAddress = request.socket.remoteAddress?.replace("::ffff:", "") ?? "unknown"; ^ SyntaxError: Unexpected token '.' at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18) at async link (internal/modules/esm/module_job.js:42:21)

We had the same issue after installing node on the Debian Server but after updating it with nvm install 19.4.0it fixed the problem

The command serve -s build did work then on the server but it does not work in the pipeline.

We are discussing the possibility that the container is using is his own environment but we are not sure with that assumption.

Can some help and explain the problem.


Solution

  • The error comes from the "new" optional chaining javascript feature ?. which is supported from v14. However default debian 11 node version is v12. Thus you should update node to newest version

    sudo apt remove node npm nodejs #remove them old node/npm
    sudo snap install node --classic #see https://snapcraft.io/node

    or use instead nvm like you have done; to install specific node version