Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR: Error reading "config\config.js". Error: undefined. Migracion con sequelize-cli #1516

Open
darwinrr71 opened this issue Jul 20, 2024 · 6 comments

Comments

@darwinrr71
Copy link

darwinrr71 commented Jul 20, 2024

Hola a todos,

Tengo un problema al ejecutar la migracion con sequelize-cli, recibo el mensaje de error de lectura en el archivo config.js.
Tengo configurado variables de entorno(.env) en la raiz de mi proyecto, no tengo problemas para autenticarme a mi base de datos cuando hago "npm run dev". Estoy trabajado con modulos para las importaciones ("type": "module",).

Agradeceria toda ayuda que sea posible estoy estancado en esto.

gracias.

Mis rutas de directorios:
carpeta raiz
config
- config.js
db
- migrations
- model
- seeders
.env
.sequelizerc
app.js
index.js
...

/* package.json */
"type": "module",
"dependencies": {
"dotenv": "^16.4.5",
"pg": "^8.12.0",
"sequelize": "^6.37.3"
}
"devDependencies": {
"sequelize-cli": "^6.6.2"
}

/* config.js */
import dotenv from "dotenv"
import { Sequelize } from "sequelize"
dotenv.config()

export const development = new Sequelize({
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: "postgres",
});

export const test = new Sequelize({
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: "postgres",
});

export const production = new Sequelize({
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: "postgres",
});

/* .sequelizerc*/
const path = require('path');

module.exports = {
config: path.resolve('config', 'config.js'),
'models-path': path.resolve('db', 'models'),
'seeders-path': path.resolve('db', 'seeders'),
'migrations-path': path.resolve('db', 'migrations'),
};

@darwinrr71 darwinrr71 changed the title ERROR: Error reading "config\config.js". Error: undefined ERROR: Error reading "config\config.js". Error: undefined. Migracion con sequelice-cli Jul 20, 2024
@darwinrr71 darwinrr71 changed the title ERROR: Error reading "config\config.js". Error: undefined. Migracion con sequelice-cli ERROR: Error reading "config\config.js". Error: undefined. Migracion con sequelize-cli Jul 20, 2024
@aobregonmx
Copy link

Note that the configuration file .sequelizerc uses commonjs, if possible change your project to commonjs
I had problems executing migrations using ESM.

Can you add error message and trace?

@darwinrr71
Copy link
Author

Gracias por tu respueta,

Si, lo cambie a commonjs y puedo aun autenticarme sin problemas, ahora arroja un problema sobre el dialecto : "ERROR: The dialect [object Object] is not supported. Supported dialects: mssql, mariadb, mysql, oracle, postgres, db2 and sqlite.", aun cuando lo estoy definiendo en mi archivo de configuracion.

`const dotenv = require("dotenv")
dotenv.config()
const Sequelize = require("sequelize")

const development = new Sequelize({
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: "postgres",
});

module.exports = development`

@aobregonmx
Copy link

Your sequelize-cli configuration file config/config.js should have this structure:

module.exports = {
  development: {
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'postgres',
  },
  test: {
    username: 'root',
    password: null,
    database: 'database_test',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
  production: {
    username: 'root',
    password: null,
    database: 'database_production',
    host: '127.0.0.1',
    dialect: 'postgres',
  },
};

Notice that a Sequelize instance is not used
I hope this has been useful to you

@darwinrr71
Copy link
Author

Bien,

Si lo hago de esa forma, como tendria que llamar a "Developmet" en mi index para cargar las base de datos?

gracias.

@darwinrr71
Copy link
Author

Finalmente lo pude hacer, cambie a module.exports mi config.js y agregue un archivo de configuracion mas db.js en la carpeta config que contiene:

/** db.js**/
const config = require("./config.js")
const { Sequelize } = require("sequelize")
const env = process.env.NODE_ENV || "development"
const development = new Sequelize(config[env])

module.exports = development

Luego lo llamo desde mi index:

const app = require("./app.js")
const development = require("./config/db.js")
const dotenv = require("dotenv")
dotenv.config()

const port = process.env.PORT || 6000
async function main() {
    try {
        await development.authenticate()
        console.log("Connection has been established successfully.")
        app.listen(port, () =>
            console.log(`The server is running on port:${port}`)
        )
    } catch (error) {
        console.error("Unable to connect to the databas")
    }
}
main()

Por alguna razon parece que el Sequelize no toma bien las variable de entorno .env, con esta configuracion funciona la autenticacion y la migracion.

@aobregonmx
Copy link

There are two configurations, one for your app to listen to requests and another to execute the sequelize-cli commands

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants