Cannot find module 'express' - node.js

I'm using docker-compose to run node.js express app.
this is my Dockerfile
FROM node:latest
MAINTAINER jorge#valeet.io
# set default workdir
WORKDIR /src/api
# Expose the application port and run application
EXPOSE 3015
CMD [ "node" , "app.js"]
docker-compose.yml
api:
build: .
volumes:
- .:/src/api
- /src/api/node_modules
links:
- mongo
ports:
- "3015:3015"
mongo:
image: mongo:3.2
ports:
- "27017:27017"
running:
docker-compose build
docker-compose up -d
I get an error on the the node.js app
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:339:15)
at Function.Module._load (module.js:290:25)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/src/api/app.js:4:15)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
I tried reinstall express, delete all packages, adding node_modules as volume in the docker-compose (the actual setup). I don't know what else to do.
I'm running it on a mac just in case it matters
Thanks!

Related

Can't Make Strapi API Requests Receiving error: Module Did not self-register

I'm unable to make any API using the Strapi api without getting the below error message.
/Users/xxxxxxxxxxxxx/node_modules/snappy/build/Release/binding.node'.
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1144:18)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:101:18)
at bindings (/Users/xxxxxxxxxxxxx/node_modules/bindings/bindings.js:112:48)
at Object.<anonymous> (/Users/xxxxxxxxxxxxx/node_modules/snappy/snappy.js:2:34)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at Object.require (internal/modules/cjs/helpers.js:101:18)
at _optionalRequire (/Users/xxxxxxxxxxxxx/Documents/xxxxxxxxxxx/node_modules/mongodb/node_modules/optional-require/dist/index.js:111:65)
at x (/Users/xxxxxxxxxxxxx/Documents/xxxxxxxxxxx/node_modules/mongodb/node_modules/optional-require/dist/index.js:172:16)
at Object.retrieveSnappy (/Users/xxxxxxxxxxxxx/Documents/xxxxxxxxxxx/node_modules/mongodb/lib/core/connection/utils.js:49:18)
at Object.<anonymous> (/Users/xxxxxxxxxxxxx/Documents/xxxxxxxxxxx/node_modules/mongodb/lib/core/wireprotocol/compression.js:3:47)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
This error shows up even when I simply do the following:
Run npx create-strapi-app#latest my-project --quickstart
Make a Postman request to /api/auth/local/register/
I expect a user to get created and have a token returned. Instead the above error is shown in the console and a 500 internal error is returned in Postman.
System
Node.js version: v14.19
NPM version:6.14.13
Strapi version: 4.0.7
Database: I've tried with Postgres and SQLLite
Operating system: MacOS 11.6
I've read all the StackOverflow posts regarding this and I've deleted and reinstalled the node_modules file numerous times. I've created multiple Strapi Projects and the same thing happens on all of them.
The interesting part is that I setup one instance with a Dockerfile and deployed this instance to ElasticBeanstalk with a database connecting to an RDS instance.
FROM strapi/base
WORKDIR /strapi-app
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install
COPY . .
ENV NODE_ENV development
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
When I try to run the API commands on that instance they work without an issue. I've exhausted all resources I can think of and I'm still unable to resolve this issue.
UPDATE:
I've since removed all other versions from NVM.
nvm list
v14.19.0
system
I also went out to nodejs.org and downloaded the latest package so my system is using v16.13.2.
Once again removed node_modules and reinstalled. Still the same problem.
I have this in my .bash_profile
###NVM CONFIG###
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && . "/usr/local/opt/nvm/etc/bash_completion" #This loads nvm bash_completion
I came across the same problem, try this
npm install snappy

Nestjs Yarn build failed Error: Cannot find module './_assignValue'

I tried to start a new project using nestjs and yarn after I run yarn install which is compeleted successfully I run yarn build and here is what I got
The Error :
$ rimraf dist
$ nest build
internal/modules/cjs/loader.js:888
throw err;
^
Error: Cannot find module './_assignValue'
Require stack:
- /home/ashraf/test-project/node_modules/lodash/_copyObject.js
- /home/ashraf/test-project/node_modules/lodash/assignIn.js
- /home/ashraf/test-project/node_modules/lodash/extend.js
- /home/ashraf/test-project/node_modules/inquirer/lib/ui/baseUI.js
- /home/ashraf/test-project/node_modules/inquirer/lib/ui/bottom-bar.js
- /home/ashraf/test-project/node_modules/inquirer/lib/inquirer.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/lib/utils/project-utils.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/actions/generate.action.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/actions/index.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/commands/command.loader.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/commands/index.js
- /home/ashraf/test-project/node_modules/#nestjs/cli/bin/nest.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Module.require (internal/modules/cjs/loader.js:957:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/ashraf/test-project/node_modules/lodash/_copyObject.js:1:19)
at Module._compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module._load (internal/modules/cjs/loader.js:774:14)
at Module.require (internal/modules/cjs/loader.js:957:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/ashraf/test-project/node_modules/lodash/_copyObject.js',
'/home/ashraf/test-project/node_modules/lodash/assignIn.js',
'/home/ashraf/test-project/node_modules/lodash/extend.js',
'/home/ashraf/test-project/node_modules/inquirer/lib/ui/baseUI.js',
'/home/ashraf/test-project/node_modules/inquirer/lib/ui/bottom-bar.js',
'/home/ashraf/test-project/node_modules/inquirer/lib/inquirer.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/lib/utils/project-utils.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/actions/generate.action.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/actions/index.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/commands/command.loader.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/commands/index.js',
'/home/ashraf/test-project/node_modules/#nestjs/cli/bin/nest.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
In other hand if I tried using npm install and npm run build everything works fine !

"Failed to load gRPC binary module" error when running a Sails.js application inside a Docker container

I have a local Docker developer environment to build a Sails.js application. This is what my Dockerfile looks like:
FROM node:8.12
LABEL Name=us.gcr.io/my-project/my-app Version=1.0.0
# Container configuration
RUN npm install -g sails#1.0.2 grunt#1.0.3 nodemon#1.18.4
WORKDIR /usr/src/app
COPY ./server/package.json ./package.json
RUN npm install
VOLUME /usr/src/app
EXPOSE 1337
This is what my docker-compose.yml file looks like:
version: '3.4'
services:
server:
image: us.gcr.io/my-project/my-app:latest
build: .
environment:
NODE_ENV: development
IS_DEV_MACHINE: "yes"
ports:
- 1338:1337 # HOST_PORT is 1339 to avoid conflicts with other Sails.js apps running on host
volumes:
- ./server:/usr/src/app
entrypoint: nodemon
mongodb:
image: mongo:4
ports:
- 27018:27017 # HOST_PORT is 27018 to avoid conflicts with other MongoDB databases running on host
volumes:
- ../database:/data/db
Normally, everything works fine however, I recently imported and initialised the #google-cloud/logging NPM package in my application and now, when I run docker-compose up, I get the following error:
server_1 | error: Bootstrap encountered an error: (see below)
server_1 | error: Failed to lift app: { Error: Failed to load gRPC binary module because it was not installed for the current system
server_1 | Expected directory: node-v57-linux-x64-musl
server_1 | Found: [node-v57-darwin-x64-unknown]
server_1 | This problem can often be fixed by running "npm rebuild" on the current system
server_1 | Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v57-linux-x64-musl/grpc_node.node'
server_1 | at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/grpc_extension.js:53:17)
server_1 | at Module._compile (module.js:653:30)
server_1 | at Object.Module._extensions..js (module.js:664:10)
server_1 | at Module.load (module.js:566:32)
server_1 | at tryModuleLoad (module.js:506:12)
server_1 | at Function.Module._load (module.js:498:3)
server_1 | at Module.require (module.js:597:17)
server_1 | at require (internal/module.js:11:18)
server_1 | at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client_interceptors.js:145:12)
server_1 | at Module._compile (module.js:653:30)
server_1 | at Object.Module._extensions..js (module.js:664:10)
server_1 | at Module.load (module.js:566:32)
server_1 | at tryModuleLoad (module.js:506:12)
server_1 | at Function.Module._load (module.js:498:3)
server_1 | at Module.require (module.js:597:17)
server_1 | at require (internal/module.js:11:18) code: 'MODULE_NOT_FOUND' }
mongodb_1 | 2018-10-09T09:27:52.521+0000 I NETWORK [conn4] end connection 172.19.0.2:48730 (0 connections now open)
In the error above, "Bootstrap" is the bootstrap.js file in my Sails.js project that initialises #google-cloud/logging. This is how I am initialising the logger in bootstrap.js:
// Globally required packages
const { Logging } = require('#google-cloud/logging');
const logging = new Logging({ projectId: 'my-project' });
const logger = logging.log('test');
I just can't seem to figure out why this error is occurring. I even tried changing my base Docker image to the official Google App Engine Docker image (gcr.io/google-appengine/nodejs) and that did not help either. i can't find any solutions to this problem anywhere. Appreciate any help.
By any chance did you do a npm install on your local Mac (darwin) environment when it should have been done in the container (linux)? The grpc binary is for the wrong OS thus the error. This often happens with a local Mac running a linux Docker container.
Expected directory: node-v57-linux-x64-musl
Found: [node-v57-darwin-x64-unknown]
The solution would be to rm -fr node_modules and do npm install in the container.
Rebuild the app by the following command.
npm rebuild --target=8.1.0 --target_platform=linux --target_arch=x64 --target_libc=musl
It looks like the project is having difficulty finding dependencies such as the gRPC binary module (repaired with npm install grpc) or perhaps where they are currently located - more info here

Sam local template handler within sub directory

I have a file called index.js within a sub-folder called api. The template.yaml is in the root folder. I am trying to add the handler (index.js) to the template but get the following error -
Mounting /Users/......./services/api as /var/task:ro inside runtime container
START RequestId: 62e6c91c-8c27-167a-d34c-b2e7aab1d11e Version: $LATEST
Unable to import module 'index': Error
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/task/appLpVideoList.js:3:15)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
The template.yaml looks like -
VideoList:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
CodeUri: api/
Events:
EventDetail:
Type: Api
Properties:
Path: /videos
Method: get
Environment:
Variables:
NODE_ENV: local
So instead of using sam local I used serverless-offline and it solved my issue. U can find the details about serverless-offline here -
https://github.com/dherault/serverless-offline

Mongoose: Error cannot find module debug

I'm building a basic MEAN webapp and am new to the stack. I have the front end running, but as soon as I add the following lines to app.js:
var mongoose = require('mongoose');
require('./models/test');
mongoose.connect('mongodb://localhost:3000/design-data-test');
I get the following error in terminal:
Error: Cannot find module 'debug'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/username/node_modules/mongoose/node_modules/mquery/lib/mquery.js:11:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
And all of my front end code stops running. Mongodb is running on the default port.
How would I go about resolving this error?
For future visitors: You are probably missing a dependency. Make sure you run this first:
npm install
... before you run your app with npm start or node <app>
I think this may happen if you have a child dependency on debug through another package (for example express or mongoose) but you did not provide the dependencies' package.json files with the deployed application which makes node.js unable to locate debug.

Resources