hanushic
hanushic

Reputation: 389

TypeError [ERR_INVALID_URL]: Invalid URL

I have clone a nestjs project and run by using npm start. I got below error.

I am new to nestjs. Please help me to find the error

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:363:5)
    at onParseError (node:internal/url:537:9)
    at new URL (node:internal/url:613:5)
    at new HostAddress (/advice/node_modules/mongodb/src/utils.ts:1322:32)
    at fromString (/advice/node_modules/mongodb/src/utils.ts:1369:12)
    at Array.map (<anonymous>)
    at Object.parseOptions (/advice/node_modules/mongodb/src/connection_string.ts:253:43)
    at new MongoClient (/advice/node_modules/mongodb/src/mongo_client.ts:332:22)
    at advice/node_modules/mongoose/lib/connection.js:785:16
    at new Promise (<anonymous>)

Edit: Answer

Have create a file .env with

DB_USERNAME=username
DB_PASSWORD=pw
DB_DATABASE=db
DB_HOST=host
DB_PORT=port

Upvotes: 1

Views: 17419

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70510

If you're using Nest's @nestjs/config module for populating from an .env file, you should be using the async registration method like so:

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    uri: configService.get<string>('MONGODB_URI'),
  }),
  inject: [ConfigService],
});

Upvotes: 0

Related Questions