Home > front end >  TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received und
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received und

Time:12-07

import dotenv from "dotenv";
import path from 'path';

dotenv.config({
  path: path.resolve(path.__dirname, `${process.env.NODE_ENV}.env`),
});

export default config = {
  NODE_ENV: process.env.NODE_ENV || "development",
  HOST: process.env.HOST || "localhost",
  PORT: process.env.PORT || 3001,
};

When i try to make config.js file to work multiple .env files but i get this error.os : windows 10

CodePudding user response:

__dirname is not located in the path module so path.__dirname is undefined.

You should use __dirname.

See here : https://www.digitalocean.com/community/tutorials/nodejs-how-to-use__dirname#:~:text=__dirname is an environment,js project.

CodePudding user response:

i solved with that:

import dotenv from "dotenv";
import path from 'path';

dotenv.config({
  path: path.resolve(path.dirname("/"), `${process.env.NODE_ENV}.env`),
});
const config = {
  NODE_ENV: process.env.NODE_ENV || "development",
  HOST: process.env.HOST || "localhost",
  PORT: process.env.PORT || 3001,
};


export default config;
  • Related