docker-compose is encountering an issue where it is unable to recognize the node_modules directory created by npm

It seems like I'm not the first one to encounter this issue, but none of the solutions I've found seem to work for me.

Here is my docker-compose.yml file:

web:
build: .
volumes:
   - .:/src
ports:
   - "3000:3000"

And here is my Dockerfile:

FROM node:0.12

RUN npm install -g mocha

RUN mkdir /src
WORKDIR /src
ADD package.json /src/package.json
RUN npm install
COPY . /src

EXPOSE 3000
CMD node server.js

After a successful build and running via docker-compose up, I encountered the following error:

web_1 | Error: Cannot find module 'express'
web_1 |     at Function.Module._resolveFilename (module.js:336:15)
web_1 |     at Function.Module._load (module.js:278:25)
web_1 |     at Module.require (module.js:365:17)
web_1 |     at require (module.js:384:17)
web_1 |     at Object.<anonymous> (/src/server.js:1:77)
web_1 |     at Module._compile (module.js:460:26)
web_1 |     at Object.Module._extensions..js (module.js:478:10)
web_1 |     at Module.load (module.js:355:32)
web_1 |     at Function.Module._load (module.js:310:12)
web_1 |     at Function.Module.runMain (module.js:501:10)

Does anyone have any insights on what might be causing this? I've already spent over 3 hours trying to figure it out with no luck (I'm new to docker).

Answer №1

By using volume: - .:/src, it seems like you are replacing the directory with a new one.

In order to make this work, don't forget to run npm install on the host machine.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Encountering a problem with compression in Kafka while using the Node.js client with

I am currently utilizing the kafka-node library to consume data from Kafka. It appears that the data I am receiving is compressed with SNAPPY. How can I decompress this data once it has been retrieved? I attempted to use the node-snappy library for decompr ...

What causes the "command not found" errors when attempting to source an environment variable file that contains values with quotes and spaces?

Hey, this isn't your typical "command not found" situation. I've got this environment file that I put together using some bash scripting, and then I feed it into a docker-compose service definition. The file has a few variables with single quote ...

Facing issues with Express, http-proxy-middleware, and encountering the error net::ERR_CONNECTION_REFUSED

For some time now, I've been troubleshooting an issue with my Express App that utilizes http-proxy-middleware to forward requests to another backend service. The problem arises when a third-party application makes a request to my server using an IP ad ...

What is the best way to showcase an image on a server for an HTTPS client?

When the server (Express) is working on port 4000, there is an image located at /public/image.png Setting up Express Static File Serving has been completed The client (Next.js) is running on port 3000 During local testing, <image src="http://loca ...

The absence of responseJSON in the jquery ajax response is causing an issue

Currently, I am developing a small web framework for conducting an HCI study and have encountered the following issue: In my setup, I have a Node server running with Express to serve local host data from JSON files. While it may not be the most advanced d ...

Error: The process.binding feature is not supported in the current environment (browserify + selenium-webdriver)

Recently, I've been attempting to execute a Node.js code on the client side of my browser. To make my code compatible with browsers, I am using Browserify for conversion purposes. Below is the command I use for this transformation: browserify te ...

Executing code following the installation of an NPM module

Can a post-processing script be executed automatically after installing an NPM module? In my node.js project, I encounter situations where I need to install specific NPM modules. Upon installation of these modules, there is a requirement to automatically ...

The mysql.query function is failing to insert data into a table that is located outside of its defined

Having trouble pushing data to my sortedPlayers array in my SQL query function. Here's the issue: async function test (score, users, con) { let playerNames = []; let sortedScore = score.sort(function(low, high) {return high-low}); ...

Is there a way to verify that a promise has been successfully awaited, rather than just created, using Sinon?

Imagine having a function: const someAction = async(): Promise<string> => { /* do stuff */ }; And there is code that simply needs to execute this action without caring about the result. However, there is a mistake - the action is not set to ...

Making an npm request with specified headers and authorization

My current task involves accessing an API using the "request" npm package. The API requires a header called "content-type" and basic authentication. Here is the progress I have made so far: var request = require('request'); var options = { url ...

The error message "this is undefined" is triggered by the value tag in the Material UI SearchBar component

I am facing an issue with the example usage of material-UI's <SearchBar /> component. Here is the code snippet I am trying to execute: import SearchBar from "material-ui-search-bar"; function App() { return ( <SearchBar ...

The module '@algolia/cache-common' is missing and cannot be located

summary: code works locally but not in lambda. My AWS lambda function runs perfectly when tested locally, utilizing Algolia within a service in the server. Despite installing @algolia/cache-common, any call to the lambda results in a crash due to the erro ...

What is the best way to send a request to an internal API using Express in a node.js application

In the past, I defined an API in this way. app.get('/login', function(req, res){ //Implementing the API }); Now, I have a new part of the API that requires using the login API for validation. app.get('/changePassword', function(req ...

The command 'typings' is not recognizable as an internal or external command, executable program, or batch file

update: ever since this question was posted, the use of typings has been phased out in favor of @types. Overview I'm attempting to set up typescript typings for my web application, but I'm encountering an issue where the command prompt does n ...

Encountering an issue while trying to verify user registration in MySQL using Node.js Express. During the user registration process, an error arises indicating that 'res' is not defined

import React from 'react' import { Link } from 'react-router-dom' import {useNavigate} from 'react-router-dom'; import { useState } from 'react' axios from "axios" const Register = () => { const [i ...

When using RS256 with JWT, the private key will not be accepted

I've been attempting to generate a JWT using 'jsonwebtoken' with RS256. The keys were generated using the following command: ssh-keygen -t rsa -b 4096 -m PEM -f <filename> The private key output appears as follows: -----BEGIN RSA PRIV ...

Issue with error handling in Node and MongoDB when using Express, Mongoose, and the 'mongoose-unique-validator' plugin

I am facing an issue with the 'mongoose-unique-validator' plugin when trying to handle Mongo ValidationError in my custom error handler. Despite other errors being handled correctly, this specific one is not triggering the desired response from m ...

What could be causing my NestJS library to only function properly when installed from NPM?

I have encountered a peculiar issue while developing my NestJS library. When testing the library by linking it to a simple NestJS project using yarn link, I consistently face errors upon starting the project. Even after removing the code lines causing the ...

Tips for parsing BSON data using body parser in Express.js

I am currently working on a Node.js API utilizing Express.js with body parser to handle a BSON binary file sent from a python client. Below is the code snippet from the Python client: data = bson.BSON.encode({ "some_meta_data": 12, "binary_data": ...

Communicating data transfer between two Node.js servers through the use of the Node Serial Port technology

How can I send the message "Hello world" from one nodejs server to another using node-serialport? I have confirmed that the radios connecting the two servers are properly connected as they are displaying buffer information after running my current code. ...