Sequelize-cli db:create Cannot read properties of undefined (reading 'replace') when creating DB

77 Views Asked by At

I was using sequelize-cli to create postgresql DB using the command: npx sequelize-cli db:create. My config.js file is

const dotenv = require('dotenv')
dotenv.config()

module.exports = {
    development: {
        username: process.env.PG_USER,
        password: process.env.PG_PASS,
        database: process.env.PG_DB,
        host: process.env.PG_HOST,
        dialect: 'postgres',
    }
}

I was getting the below error

TypeError: Cannot read properties of undefined (reading 'replace')
    at Object.removeTicks (/../node_modules/sequelize/lib/utils.js:281:12)
    at PostgresQueryGenerator.quoteIdentifier [as _quoteIdentifier] (/../node_modules/sequelize/lib/dialects/postgres/query-generator.js:663:33)
    at PostgresQueryGenerator.quoteIdentifier (/../node_modules/sequelize/lib/dialects/abstract/query-generator.js:696:19)
    at getCreateDatabaseQuery (/../node_modules/sequelize-cli/lib/commands/database.js:60:50)
    at exports.handler (/../node_modules/sequelize-cli/lib/commands/database.js:37:17)
1

There are 1 best solutions below

0
Foysal Khandakar Joy On

I figured out, the .env file was not loaded properly from dotenv.config() due to path issue as .env was in different directory. That's why I got the error. I solved it by setting path like this in config.js file.

dotenv.config({ path: '../.env' })

So, if you face similar issue, specify your .env path.