Cannot run production build - nestjs

I am developing API with nestjs and TypeORM connected with PostgreSQL. NestJS is using ormconfig.json file to provide database connection.
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "xyz",
"password": "xyz",
"database": "xyz",
"entities": ["src/**/**.entity{.ts,.js}"],
"synchronize": true
}
When in dev mode npm run start:dev everything works OK, API is responding, connected properly with database. But when I try run production build: npm run prestart:prod followed by npm run start:prod something breaks and program cannot connect to the database, that cause error:
[Nest] 12444 - 2019-04-09 22:19 [NestFactory] Starting Nest application...
[Nest] 12444 - 2019-04-09 22:19 [InstanceLoader] TypeOrmModule dependencies initialized +85ms
[Nest] 12444 - 2019-04-09 22:19 [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 12444 - 2019-04-09 22:19 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +111ms
C:\_code\sensorhub\packages\api-nestjs\src\installations\models\installation.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
[Nest] 12444 - 2019-04-09 22:19 [TypeOrmModule] Unable to connect to the database. Retrying (2)... +3130ms
installation.entity.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
#Entity()
export class InstallationEntity {
#PrimaryGeneratedColumn()
id: number;
#Column({ length: 500 })
name: string;
#Column('text')
description: string;
}
I think that code itself is correct, but there is something wrong with proper configuration.
nestjs ver.6.0.0

try this one: https://github.com/nestjs/nest/issues/184
using a dynamic ormconfig.js (instead of .json)

Related

ts-node cannot find npm module discord-api-types

When attempting to run discord.js file deploy-commands.ts with the command ts-node deploy-commands.ts ts-node produces the following error:
Error: Cannot find module 'discord.js/node_modules/discord-api-types'
Require stack:
- /Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/lib/node_modules/ts-node/node_modules/#cspotcode/source-map-support/source-map-support.js:679:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1458:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts' ]
}
Despite discord-api-types appearing both in the node-modules folder and in package.json. The error is consistently reproduced after running npm install discord-api-types and npm resoding discord-api-types is up-to-date.
Package.json dependencies:
"dependencies": {
"#discordjs/builders": "^0.12.0",
"#discordjs/rest": "^0.3.0",
"discord-api-types": "^0.29.0",
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0"
}
deploy-commands.ts:
import { SlashCommandBuilder } from "#discordjs/builders";
import { REST } from "#discordjs/rest";
import { version } from "discord.js";
import { Routes } from "discord.js/node_modules/discord-api-types";
const { token, guildID, clientID } = require('./process.json')
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong.'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info.'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info.'),
]
.map(commands => JSON)
const rest = new REST({version: '9'}).setToken(token)
rest.put(Routes.applicationCommand(clientID, guildID), {body: { commands }})
.then(() => {
console.log('Successfully registered application commands')
})
.catch(console.error)
I think you need to change this code line:
import { Routes } from "discord.js/node_modules/discord-api-types";
To its version using because of this:
const rest = new REST({version: '9'}).setToken(token)
So you need to define the version you use, try changing your import to
import { Routes } from "discord-api-types/v9";
As I tried to my testing slash command files, you can do this kind of line too:
import { Routes } from "discord.js/node_modules/discord-api-types/v9";
I'm not using .ts so might not work but give it a shot

Error "SyntaxError: Cannot use import statement outside a module" when deploying React app with Netlify Functions

I deployed an app using Netlify Functions and got an error ""errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module"". How can I solve this?
I used create-react-app to make the app. I installed node-fetch and used in the Netlify Functions. I didn't install Lambda. I've tried adding "type": "module" in package.json but it didn't work
■Error on Netlify Functions Log
6:58:51 PM: 2021-09-15T09:58:51.177Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
6:58:51 PM: 2021-09-15T09:58:51.372Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
6:58:51 PM: 75585d70 Duration: 159.55 ms Memory Usage: 14 MB 6:58:51 PM: Unknown application error occurred
Runtime.UserCodeSyntaxError
6:59:05 PM: 2021-09-15T09:59:05.638Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
6:59:05 PM: 2f6dbc1c Duration: 135.91 ms Memory Usage: 14 MB 6:59:05 PM: Unknown application error occurred
Runtime.UserCodeSyntaxError
6:59:05 PM: 2021-09-15T09:59:05.845Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
6:59:23 PM: 2021-09-15T09:59:23.733Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
6:59:23 PM: 7b7c0366 Duration: 135.01 ms Memory Usage: 14 MB 6:59:23 PM: Unknown application error occurred
Runtime.UserCodeSyntaxError
6:59:23 PM: 2021-09-15T09:59:23.928Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
■package.json
{
"type": "module",
"homepage": "http://.",
"name": "react-weather-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"node-fetch": "^3.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-icons": "^4.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"gh-pages": "^3.2.3"
}
■Netlify Functions
import fetch from 'node-fetch';
exports.handler = async function(event) {
try{
const {city} = event.queryStringParameters;
const API = process.env.REACT_APP_API_KEY;
const url= `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API}`;
const response = await fetch(url);
const data = await response.json();
console.log(data);
return {
statusCode: 200,
body: JSON.stringify({data})
}
} catch(err) {
console.log(err);
return {
statusCode: 400,
body: JSON.stringify({error: 'Failed fetching data'})
}
}
}
Change file extension of Netlify Functions from *.js to *.mjs in order to use import statements.
Alternatives to Maksim Shamihulau's answer based on the docs and netlify forum:
Use CommonJs fetch function instead
rewrite
import fetch from 'node-fetch';
as
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
Use node-fetch#2 (see forum post) with require
as
const fetch = require('node-fetch');
Overall, the file extension rewrite method by Maksim Shamihulau seems superior to both.

Firbase/Heroku/Node JS - Firebase Invalid Credential / failed to parse service account json file / ENAMETOOLONG

I'm using github actions to deploy my first webapp via Heroku.
My code:
const fireBaseAdmin = require('firebase-admin');
if (!fireBaseAdmin)
throw new Error('The FIREBASE_SERVICE_ACCOUNT_CREDS environment variable was not found!');
fireBaseAdmin.initializeApp({
"credential": fireBaseAdmin.credential.cert(JSON.stringify({
"type": process.env.FIREBASE_TYPE,
"project_id": process.env.FIREBASE_PROJECT_ID,
"private_key_id": process.env.FIREBASE_PRIVATE_KEY_ID,
"private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
"client_email": process.env.FIREBASE_CLIENT_EMAIL,
"client_id": process.env.FIREBASE_CLIENT_ID,
"auth_uri": process.env.FIREBASE_AUTH_URI,
"token_uri": process.env.FIREBASE_TOKEN_URI,
"auth_provider_x509_cert_url": process.env.FIREBASE_AUTH_PROVIDER,
"client_x509_cert_url": process.env.FIREBASE_CLIENT
}))
});
module.exports = fireBaseAdmin;
Each of the .env variables are stored in Heroku's Config Vars.
Error log from Heroku (I stripped sensitive tokens/keys):
2021-08-17T11:16:53.818119+00:00 app[web.1]: /app/node_modules/firebase-admin/lib/credential/credential-internal.js:151
2021-08-17T11:16:53.818149+00:00 app[web.1]: throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse service account json file: ' + error);
2021-08-17T11:16:53.818150+00:00 app[web.1]: ^
2021-08-17T11:16:53.818150+00:00 app[web.1]:
2021-08-17T11:16:53.818166+00:00 app[web.1]: FirebaseAppError: Failed to parse service account json file: Error: ENAMETOOLONG: name too long, open '{
"type":"service_account",
"project_id":"ec31",
"private_key_id":"2900d",
"private_key":"-----BEGIN PRIVATE KEY-----\nQ=\n-----END\nPRIVATE KEY-----\n",
"client_email":"firebase-adminsdk-.gserviceaccount.com",
"client_id":"10",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"ttps://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o.gserviceaccount.com"
}'
2021-08-17T11:16:53.818167+00:00 app[web.1]: at FirebaseAppError.FirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:44:28)
2021-08-17T11:16:53.818168+00:00 app[web.1]: at FirebaseAppError.PrefixedFirebaseError [as constructor] (/app/node_modules/firebase-admin/lib/utils/error.js:90:28)
2021-08-17T11:16:53.818168+00:00 app[web.1]: at new FirebaseAppError (/app/node_modules/firebase-admin/lib/utils/error.js:125:28)
2021-08-17T11:16:53.818169+00:00 app[web.1]: at Function.ServiceAccount.fromPath (/app/node_modules/firebase-admin/lib/credential/credential-internal.js:151:19)
2021-08-17T11:16:53.818169+00:00 app[web.1]: at new ServiceAccountCredential (/app/node_modules/firebase-admin/lib/credential/credential-internal.js:67:28)
2021-08-17T11:16:53.818174+00:00 app[web.1]: at Object.exports.cert (/app/node_modules/firebase-admin/lib/credential/credential.js:34:54)
2021-08-17T11:16:53.818174+00:00 app[web.1]: at Object.<anonymous> (/app/firebase/index.js:25:42)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1072:14)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
2021-08-17T11:16:53.818175+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:937:32)
2021-08-17T11:16:53.818176+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:778:12)
2021-08-17T11:16:53.818176+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:961:19)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:92:18)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at Object.<anonymous> (/app/middlewares/auth.js:1:15)
2021-08-17T11:16:53.818177+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1072:14)
2021-08-17T11:16:53.818178+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) {
2021-08-17T11:16:53.818178+00:00 app[web.1]: errorInfo: {
2021-08-17T11:16:53.818178+00:00 app[web.1]: code: 'app/invalid-credential',
2021-08-17T11:16:53.818190+00:00 app[web.1]: message: `Failed to parse service account json file: Error: ENAMETOOLONG: name too long, open '{
"type":"service_account",
"project_id":"e1",
"private_key_id":"2900",
"private_key":"-----BEGIN PRIVATE KEY-----\\nMIIEMoQ=\\n-----END\\nPRIVATE KEY-----\\n",
"client_email":"firebase-adminsdk-od.gserviceaccount.com",
"client_id":"1",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"ttps://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o.gserviceaccount.com"
}'`
2021-08-17T11:16:53.818190+00:00 app[web.1]: },
2021-08-17T11:16:53.818190+00:00 app[web.1]: codePrefix: 'app'
2021-08-17T11:16:53.818191+00:00 app[web.1]: }
2021-08-17T11:16:53.947406+00:00 heroku[web.1]: Process exited with status 1
2021-08-17T11:16:54.345497+00:00 heroku[web.1]: State changed from starting to crashed
I've been attempted to update my code based on the various posts on Stack Overflow but I'm not having success with solving the error. Here's a short list of the post I've been referencing:
Deploying Firebase App with Service Account to Heroku (environment variables with dotenv)
Authenticating with the Firebase Admin SDK using environment variable
Node.js -Firebase Service Account Private Key won't parse
Firebase: Failed to parse service account: 'project_id' must be set
Any assistance would be greatly appreciated!
The cert() method expects either the path to serviceAccount.json file (string) or a ServiceAccount object. Here you are entering a string so it's expecting that to be the path to serviceAccount.json and not stringified credentials and hence you get the error FirebaseAppError: Failed to parse service account json file. Try this:
fireBaseAdmin.initializeApp({
credential: admin.credential.cert({
type: process.env.FIREBASE_TYPE,
project_id: process.env.FIREBASE_PROJECT_ID,
private_key_id: process.env.FIREBASE_PRIVATE_KEY_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
client_email: process.env.FIREBASE_CLIENT_EMAIL,
client_id: process.env.FIREBASE_CLIENT_ID,
auth_uri: process.env.FIREBASE_AUTH_URI,
token_uri: process.env.FIREBASE_TOKEN_URI,
auth_provider_x509_cert_url: process.env.FIREBASE_AUTH_PROVIDER,
client_x509_cert_url: process.env.FIREBASE_CLIENT
}),
databaseURL: `https://${process.env.FIREBASE_PROJECT_ID}.firebaseio.com/`
})

MongoParseError: Invalid message size: 1347703880, max allowed: 67108864 (NestJS & MongoDB Docker Container)

I am working on a NestJS Project that is based on micro-service architecture (Kafka, CQRS Patter).
I have created a shared mongodb database inside docker container and exposed the port to 8081 Port which is accessible via browser(I am using mongo-express ui). Here is the docker-compose.yml file
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: PWD
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
volumes:
- data-volume:/data/db
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: PWD
volumes:
data-volume:
And in my NESTJS Service, I have defined mongo connection like this: (app.module.ts)
import { Module } from '#nestjs/common';
import { APP_FILTER } from '#nestjs/core';
import { CqrsModule } from '#nestjs/cqrs';
import { MongooseModule } from '#nestjs/mongoose';
import { UserModule } from './user/user.module';
#Module({
imports: [UserModule, MongooseModule.forRoot('mongodb://localhost:8081/urooj', {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true
})],
controllers: []
})
export class AppModule {}
And in my user module I am defining User schema: (user.module.ts)
import { Module } from '#nestjs/common';
import { CqrsModule } from '#nestjs/cqrs';
import { UserController } from './user.controller';
import { UserHandler } from "./command/handler/index";
import { MongooseModule } from '#nestjs/mongoose';
import { UserSchema } from './models/user.model';
import { UsersRepository } from './user.repository';
import { UserService } from './user.service';
#Module({
imports: [
CqrsModule,
MongooseModule.forFeature([{name: 'User', schema: UserSchema}])
],
controllers: [UserController],
providers: [
...UserHandler,
UserService
]
})
export class UserModule {}
But I am getting this error which I tried finding the solution but i am bit unlucky in solving this issue from quite sometime now.
events.js:292
throw er; // Unhandled 'error' event
^
MongoParseError: Invalid message size: 1347703880, max allowed: 67108864
at processIncomingData (/Users/rk/Desktop/workspace/project/service/node_modules/mongodb/lib/cmap/message_stream.js:118:7)
at MessageStream._write (/Users/rk/Desktop/workspace/project/service/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at MessageStream.Writable.write (_stream_writable.js:318:11)
at Socket.ondata (_stream_readable.js:716:22)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on MessageStream instance at:
at errorOrDestroy (internal/streams/destroy.js:108:12)
at MessageStream.onerror (_stream_readable.js:752:7)
at MessageStream.emit (events.js:315:20)
at errorOrDestroy (internal/streams/destroy.js:108:12)
at onwriteError (_stream_writable.js:418:5)
at onwrite (_stream_writable.js:445:5)
at processIncomingData (/Users/rk/Desktop/workspace/project/service/node_modules/mongodb/lib/cmap/message_stream.js:117:5)
at MessageStream._write (/Users/rk/Desktop/workspace/project/service/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
[... lines matching original stack trace ...]
at Socket.Readable.push (_stream_readable.js:212:10)
Thank you for the help in advance.
I fixed this above issue by Exposing Port to mongo service in my docker-compose.yml apart from mongo-express service.
But I ran into another issue now and that is related to authentication when connecting to mongodb using nestjs. Though Mongodb Container can be accessed using compass. I will be debugging this and if required will raise a new question in SO.

Cannot find module

I have a class that I have created and put it in a custom folder in the src folder. It is not a nestjs module nor a provider just a helper class that I can instantiate and use. However when I run the app I got the following error:
npm run start:dev
> rent-go#1.0.0 start:dev /Users/arsenegandote/Applications/js/rent-go
> nodemon
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/arsenegandote/Applications/js/rent-go/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
Error: Cannot find module './json.result.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._resolveFilename (/Users/arsenegandote/Applications/js/rent-go/node_modules/tsconfig-paths/lib/register.js:75:40)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/arsenegandote/Applications/js/rent-go/src/lib/api.controller.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Module.m._compile (/Users/arsenegandote/Applications/js/rent-go/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/arsenegandote/Applications/js/rent-go/node_modules/ts-node/src/index.ts:442:12)
[nodemon] app crashed - waiting for file changes before starting...](url)
This is the generic class I am instantiating in a controller:
export class JsonResult<T> {
readonly status: string;
readonly data: T;
constructor(status: string, data: T) {
this.status = status;
this.data = data;
}
}
And my controller implements this abstract class:
export abstract class ApiController {
failed(): void {
throw new HttpException(
{
status: ApiStatus.FAILED,
data: {},
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
notFound(): void {
throw new HttpException(
{
status: HttpStatus.NOT_FOUND,
data: [],
},
HttpStatus.NOT_FOUND,
);
}
badRequest(message: string): void {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
data: {
message,
},
},
HttpStatus.BAD_REQUEST,
);
}
success<T>(data: T): JsonResult<T> {
return new JsonResult<T>(ApiStatus.SUCCESS, data);
}
}
Error: Cannot find module './json.result.js'
The error means that your file cannot be found. Make sure your import path is correct.

Resources