March 22, 2021 By Kazuki Nobutani 2 min read

Three important tips to follow if you’re going to develop an app based on Node.js with IBM Cloud Code Engine.

IBM Cloud offers a new feature called IBM Cloud Code Engine — a fully managed, serverless platform that runs your containerized workloads, including web apps, microservices, event-driven functions or batch jobs. The Code Engine experience is designed so that you can focus on writing code and not on the infrastructure that is needed to host it.

In this blog, I will share what I learned through the development of an app based on Node.js. Some tips only apply to Node.js apps, and I assume you have reviewed Writing a Dockerfile for Code Engine already. 

Note: Code Engine is a beta service as of March 2021. This service might not yet be stable and is not for production use.

Prerequisites

1. Default port of Code Engine is 8080

 If you want to use non-default port other than 8080, specify the following line in the Dockerfile and run an `ibmcloud ce` command to change the port:

  • In Dockerfile:
    EXPOSE 8081
  • ibmcloud ce command:
    $ ibmcloud ce application update --name APP_NAME -p 8081

2. Make sure you have npm install in Dockerfile

This is a Node.js-specific practice. It is always safe to specify this in Dockerfile:

RUN npm install

During development, npm install is often performed and the required dependencies are imported in the node_modules, which are stored in the Git repository root directly. In the Dockerfile, the COPY . . will then use those dependencies for the build steps in the Dockerfile. In Code Engine, the build starts with a fresh Git clone, and, therefore, the node_modules is missing. As a result, you might see an error like the one below upon deploying the app:

Mar 8 16:21:10 Code Engine APP-XXX-deployment-YYY 2021-03-08T05:21:07.096108821Z stderr F Error: Cannot find module 'ZZZZ'

3. Do not carry node_modules 

This is also a Node.js-specific practice and the goal is the same — to keep everything clean and tidy:

  • In the Git repository where your code exists, create .dockerignore and add node_modules to avoid the accidental pick-up of local files. Whatever is in this file won’t be included upon build.
  • In the Git repository, create .gitignore to avoid node_modules on git gets updated upon push.

The key takeaway here is that you should always have Tips 2 and 3 in your code so that you can prevent your Docker image from getting created based on the node_modules on the host side. Make sure npm install is executed inside the Docker image.

Having a trouble deploying an app? 

Please feel free to contact IBM client support for any questions or concerns. 

Was this article helpful?
YesNo

More from Cloud

Bigger isn’t always better: How hybrid AI pattern enables smaller language models

5 min read - As large language models (LLMs) have entered the common vernacular, people have discovered how to use apps that access them. Modern AI tools can generate, create, summarize, translate, classify and even converse. Tools in the generative AI domain allow us to generate responses to prompts after learning from existing artifacts. One area that has not seen much innovation is at the far edge and on constrained devices. We see some versions of AI apps running locally on mobile devices with…

IBM Tech Now: April 8, 2024

< 1 min read - ​Welcome IBM Tech Now, our video web series featuring the latest and greatest news and announcements in the world of technology. Make sure you subscribe to our YouTube channel to be notified every time a new IBM Tech Now video is published. IBM Tech Now: Episode 96 On this episode, we're covering the following topics: IBM Cloud Logs A collaboration with IBM watsonx.ai and Anaconda IBM offerings in the G2 Spring Reports Stay plugged in You can check out the…

The advantages and disadvantages of private cloud 

6 min read - The popularity of private cloud is growing, primarily driven by the need for greater data security. Across industries like education, retail and government, organizations are choosing private cloud settings to conduct business use cases involving workloads with sensitive information and to comply with data privacy and compliance needs. In a report from Technavio (link resides outside ibm.com), the private cloud services market size is estimated to grow at a CAGR of 26.71% between 2023 and 2028, and it is forecast to increase by…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters