Docker build error: Cannot find module '/app/main'
  • 07-May-2023
Lightrun Team
Author Lightrun Team
Share
Docker build error: Cannot find module '/app/main'

[BUG] Docker build error: Cannot find module ‘/app/main’

Lightrun Team
Lightrun Team
07-May-2023

Explanation of the problem

Environment:
The issue was encountered on a Windows 10 Enterprise operating system. The application in question was deployed locally using Docker version 19.03.8, build afacb8b. The specific version of Formio.js that was used in the application was 2.0.0-beta.4.

Hosting type:
The application was hosted locally, using Docker. To reproduce the issue, the user had to install Docker tools and navigate to the Formio directory. Once in the directory, the user had to run the following commands in the terminal: “docker build -t dockerid/formio-server .” and “docker run -it –rm dockerid/formio-server”.

Expected behavior:
The expected behavior was for the API server to initialize properly and display the message “Initializing API Server. > Mongo connection established. > Serving the Form.io API Platform at http://localhost:3005” in the console.

Observed behavior:
Instead of the expected behavior, the user encountered an error message in the console: “internal/modules/cjs/loader.js:985 throw err; ^ Error: Cannot find module ‘/app/main’ at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15) at Function.Module._load (internal/modules/cjs/loader.js:864:27) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47 { code: ‘MODULE_NOT_FOUND’, requireStack: [] }”.

 

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for [BUG] Docker build error: Cannot find module ‘/app/main’

It appears that the root cause of the issue might be in the Dockerfile, specifically in the ENTRYPOINT command. One of the answers suggests that the /main part of the error might come from the ENTRYPOINT specified in the Dockerfile. This could be a hint that the command specified in ENTRYPOINT might not be correctly pointing to the main file of the application. If this is the case, updating the Dockerfile with the correct command could potentially solve the problem.

If the Dockerfile and package.json files are correct, and the issue persists, it might be worth rebuilding the container images. One of the answers suggests running docker-compose down -v to remove all volumes, followed by starting the containers again with docker-compose up. This could be useful to ensure that the container images are built correctly from scratch and that no cached artifacts or volumes are causing any issues.

In summary, if encountering the “Cannot find module” error while running a Docker container, it might be worth investigating the Dockerfile and package.json files to ensure that they are correctly pointing to the main file of the application. Additionally, rebuilding the container images and removing all volumes using docker-compose down -v could potentially solve the issue.

 

Other popular problems with formio

Problem: Unable to connect to Mongo DB

One common problem that developers face while working with Form.io is the inability to connect to the Mongo database. This issue occurs when the application cannot find the Mongo server, which leads to the “connection refused” error. The problem may arise due to incorrect credentials, network configuration, or outdated software versions.

Solution:

To resolve this issue, developers should check if they have entered the correct Mongo credentials, such as the username and password. Also, they need to make sure that their Mongo server is up and running. Developers can verify the server status by running the “systemctl status mongod” command. If the server is down, they can start it using the “sudo service mongod start” command. If the problem persists, developers may need to update their software versions to the latest release.

Problem: CORS errors while accessing APIs

Another issue that developers often face while working with Form.io is the CORS error that occurs when trying to access APIs. This error happens because the web browser restricts cross-origin requests to protect user data. As a result, developers are unable to fetch data from Form.io APIs.

Solution:

To resolve this issue, developers need to configure the Form.io server to accept cross-origin requests. This can be done by adding the appropriate headers to the server response. Developers can do this by adding the following code to the server.js file:

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

Problem: Unable to upload files

The inability to upload files is another issue that developers face while working with Form.io. This issue occurs when the upload button is clicked, and nothing happens. The problem may arise due to incorrect permissions, network configuration, or outdated software versions.

Solution:

To resolve this issue, developers should check if the server has permission to write to the file system. Developers can do this by verifying the permissions of the file system. They should also check if the file upload limit is set too low. Developers can increase the file upload limit by editing the server configuration file. If the problem persists, developers may need to update their software versions to the latest release. Additionally, they can use third-party file upload services like Amazon S3 or Google Cloud Storage to store and manage their files.

A brief introduction to formio

Form.io is a form and data management platform that enables developers to build complex web and mobile applications quickly and efficiently. It provides an easy-to-use drag-and-drop form builder that can create a variety of forms, including surveys, registration forms, contact forms, and more. Form.io also offers an extensive RESTful API that allows developers to create custom forms and data management solutions without writing any server-side code.

Form.io also provides an open-source JavaScript library called Formio.js, which can be integrated into any web application to add form creation and data management capabilities. Formio.js provides a wide range of features, including dynamic form rendering, conditional logic, validation, data encryption, and integration with third-party services like Salesforce, Dropbox, and Twilio. It also offers a powerful admin interface for managing forms, users, and data. Overall, Form.io provides a comprehensive platform for building custom forms and data management solutions, making it a popular choice among developers.

 

Most popular use cases for formio

  1. Formio can be used for rapid development of form-based applications. It offers an intuitive drag-and-drop form builder that can be used to quickly create custom forms with a wide range of form fields, conditional logic, and validations. Formio also provides a powerful API platform that can be used to easily manage the data submitted through the forms, integrate with other systems, and build custom workflows.
  2. Formio can be used for building complex workflows and automations by leveraging its API platform. Developers can create custom APIs that interact with form data and external systems, allowing for complex business logic and integrations. Additionally, Formio provides a Rules Engine that can be used to define complex conditions and actions that are triggered based on form submissions or API events.
  3. Formio can be used to create custom integrations with other systems by leveraging its API-first approach. Developers can use Formio’s API to connect to external systems, such as databases, CRMs, and marketing automation tools. For example, the following code block shows how to use Formio’s API to retrieve data submitted through a form and push it to a Google Sheet:
const formio = require('formio-client');
const googleapis = require('googleapis');

const formioClient = new formio('https://your-formio-domain.com');
const jwt = 'your-formio-jwt-token';

async function syncFormDataToGoogleSheet() {
  const formId = 'your-formio-form-id';
  const submissions = await formioClient.submission.get({ form: formId, headers: { Authorization: `Bearer ${jwt}` } });
  const rows = submissions.map(submission => Object.values(submission.data));
  
  const sheets = googleapis.sheets('v4');
  const auth = new googleapis.auth.GoogleAuth({
    keyFile: 'your-google-api-key-file.json',
    scopes: ['https://www.googleapis.com/auth/spreadsheets']
  });
  const sheetsClient = await sheets.spreadsheets.values.append({
    auth: await auth.getClient(),
    spreadsheetId: 'your-google-sheet-id',
    range: 'Sheet1!A1',
    valueInputOption: 'RAW',
    resource: {
      values: rows
    }
  });
  
  console.log(`Synced ${submissions.length} submissions to Google Sheet`);
}
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.