I'm having trouble setting up a project with the config npm package.
My config.js looks like this:
require('dotenv').config();
const { DB_HOST, DB_USERNAME, DB_PASSWORD, SENDGRID_API_KEY } = process.env;
const env = process.env.NODE_ENV;
const config = {
development: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_development",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
test: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_test",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
},
production: {
username: DB_USERNAME,
password: DB_PASSWORD,
database: "database_production",
host: DB_HOST,
dialect: "postgres",
sendgrid: {
base_url: 'https://localhost:3002',
sendgrid_api_key: SENDGRID_API_KEY,
sender_email: '',
enabled: true,
},
}
};
module.exports = config[env];
In 1 service driver file I have the following line:
const dialect = config.get('dialect');
I get the following error: "Error: Configuration property "dialect" is not defined".
I also tried using 'development.dialect' but that doesn't help either.
Is it possible that the require('config'); doesn't work?
In my Sequelize's index.js file I've got
const config = require(__dirname + '/../config/config.js'); and that seems to work fine.
This github repository provides a good example of using config package.
https://github.com/basarbk/tdd-nodejs
Notes:
for diffrent configs, you must have a file named whatever you use for NODE_ENV. for example for "start": "cross-env NODE_ENV=production node index" create a file called production.js
check config folder in root directory
if you are using window you should use cross-env.
I am follwing this thread on how to get TypeORM and PostgreSQL to work in Heroku.
Using ormconfig.js and module.exports I get this error
MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "oracle", "postgres", "sqlite", "sqljs", "react-native".
const env = require('dotenv')
env.config()
module.exports = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
The above link says switch to .ts and use export
so..using ormconfig.ts and export = config I get this error:
2020-03-25T05:07:57.946988+00:00 app[web.1]: export = config
2020-03-25T05:07:57.946988+00:00 app[web.1]: ^^^^^^
2020-03-25T05:07:57.946988+00:00 app[web.1]:
2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export'
const config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
export = config;
I've also tried using export default but had same error as with export...
Not exactly sure where to go from here...anyone had this issues?
does babel need to be included to run my node.js apps in heroku?
Do it this way to get rid of (2020-03-25T05:07:57.946989+00:00 app[web.1]: SyntaxError: Unexpected token 'export') error:
exports.config = {
name: 'default',
type: process.env.DATABASE_TYPE,
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
},
};
ormconfig MUST keep the .js extension.
Also, I am parsing the DATABASE_URL from Heroku and adding all the variables into their appropriate placeholders in ormconfig like so:
const parse = require('pg-connection-string').parse;
const env = require('dotenv')
env.config()
const config = parse(process.env.DATABASE_URL)
const pgConnection = {
type: "postgres",
host: config.host,
port: config.port,
username: config.user,
password: config.password,
database: config.database,
synchronize: true,
dropSchema: false,
logging: true,
entities: ['/src/**/*.entity.ts', 'dist/**/*.entity.js'],
extra: {
ssl: true,
}
}
module.exports = pgConnection;
I have been working on a NodeJS project which uses PostgreSQL database.
I am trying to implement migration to the database. Also, using Sequelize. After setting up the migration folder and config, it throws error while running db:migrate
The error is:
"Dialect needs to be explicitly supplied as of v4.0.0"
Solution for me was based on what I had set for my NODE_ENV variable.
echo $NODE_ENV
If you do not have anything set for that variable, try setting it with the following:
export NODE_ENV=development
If a value is present, make sure you have an entry in your config file for that value. For me, I like to use local. So I had to update my config to this:
{
local: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
development: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'postgres'
},
production: {
username: 'root',
password: null,
database: 'database',
host: '127.0.0.1',
dialect: 'postgres'
}
}
Check the dialect once.
const Sequelize = require('sequelize');
// Option 1: Passing parameters separately
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
I was facing this error, as it turns out, because of typescipt's transformation/compilation.
A little background: I am using sequelize in a typescript project. And the database config file was in a database.ts file.
const config = {
development: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
test: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
production: {
username: env.PG_USERNAME,
password: env.PG_PASSWORD,
database: 'sample_db',
host: env.PG_HOST,
port: env.PG_PORT,
dialect: 'postgres',
},
};
export default config;
In .sequelizerc file, I was pointing to the transpiled version of the database.ts file i.e. the dist/config/database.js file. As shown below:
const path = require('path');
module.exports = {
env: process.env.NODE_ENV || 'development',
config: path.resolve('dist', 'config', 'database.js'),
...
};
But after inspecting the transpiled version of the database.ts file, i noticed that the config was exported as:
module.exports.default = config
But sequelize is expecting the config to be at module.exports.
So, I modified the database.ts file by appending this one line to the end of the file and that resolved it for me.
...
module.exports = config;
I got same error and I saw this mistake in the code.
title: {
type: Sequelize,
allowNull: false,
},
Changed my code with this and problem is solved:
title: {
type: Sequelize.STRING,
allowNull: false,
},
Check your config file (env names)
{
development: {
username: 'root',
password: null,
database: 'database_development',
host: '127.0.0.1',
dialect: 'mysql'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'mysql'
},
production: {
username: 'root',
password: null,
database: 'database_production',
host: '127.0.0.1',
dialect: 'mysql'
}
}
My issue was that I wasn't pointing to the config file properly. It said "successfully loaded" but I wasn't pointing to the right file.
It should say "Loaded configuration file "[some path]/config.js"."
I think you have missed .env file in your project
In my case issue was the way I was exporting config from config.ts
export const = {} did not work
but module.exports worked:
module.exports = {
development: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
test: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
production: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
};
I had a config.js file from a previous project with a non-standard value for my environment.
It was called current.
I changed it to development and the error went away.
const main = require('./main');
module.exports = {
development: { // this was set to `current` in my case, and it was causing the error
username: main.db.user,
password: main.db.password,
database: main.db.name,
host: main.db.host,
port: main.db.port || 3306,
dialect: 'mysql'
}
};
After pulling my hair out for a couple hours, I realized I was doing cd src; node app.js when really I was supposed to do node src/app.js... Gooooooodness
After reading through all the answers and possible solutions to problems people had and nothing worked for me here is another additional solution for an environment that uses:
Typescript (Having the sequelize config in .ts)
Sequelize
Problem: Not being able to run "sequelize db:migrate" (getting the error from this thread) due to sequelize not getting access to the .env file and cannot properly read the config
Example config
config: any = {
"development": {
"username": dbUser,
"password": dbPassword,
...
module.exports = config
dbUser is undefined for Sequelize
Create a ".sequelizerc" file in the root of your project and give it the path to the already built .js config file:
const path = require('path');
module.exports = {
'config': path.resolve('./dist', 'src/config/config.js')
};
In your config file add
module.exports = config
as sequelize does not like export default config (as mentioned above). I have both types of exports.
Install the dotenv-cli (I did it as a dev dependency, but globally should work fine as well)
Run the migration command while suppling the .env file with:
npx dotenv -e /path/to/.env sequelize db:migrate
Also had to make sure my NODE_ENV is set to development:
export NODE_ENV=development
did you forget to add the dialect to your config?
see: http://docs.sequelizejs.com/manual/tutorial/migrations.html
if you have not setup any .env variables before running your npm server
You are likely to get that error. so each time you restart app for changes.
you will have to export again
export DATABASE_URL=<your-db-url>
This error can also be caused if there is an error in your model schema.
I had:
middle_name: {
type: Sequelize.Sequelize,
allowNull: false,
}
Which should had been:
middle_name: {
type: Sequelize.STRING,
allowNull: false,
}
In my case, I declared config.js like as:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: process.env.DB_DEV_DATABASE || 'database_name',
username: process.env.DB_DEV_USERNAME || 'user_name',
password: process.env.DB_DEV_PASSWORD || 'pass',
host: process.env.DB_DEV_HOST || 'localhost',
port: process.env.DB_DEV_PORT || 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
But it should be like as:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: 'database_name',
username: 'user_name',
password: 'pass',
host: 'localhost',
port: 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
without process.env.DB_DEV_DATABASE || 'database_name' eg.
If you are facing this error then,you have to add additional argument after password_for_rootUser in form of object and need to specify these property according to your RDBMS
var Sequilize=require('sequelize');
var connection =new Sequilize('db_name','root_user_name','password_for_rootUser',{
host:'localhost',
dialect:'mysql'|'mariadb'|'sqlite'|'postgress'|'mssql',
pool:{
max:5,
min:0,
idle:10000
},
//for sqlite only
storage:path/to/database.sqlite
});
var Article=connection.define('tableName',{
title:Sequilize.STRING, // your cloumn name with data type
body:Sequilize.TEXT // your cloumn name with data type
});
connection.sync();
hope this solves your problem
You can fix this by running this command
export NODE_ENV=development; npx sequelize db:migrate
cheers!
In my case, I forgot to uncomment the DB_CONNECTION value from .env which is used to establish postgreSQL connection in my code
use below code in config/config.js to load env
import dotenv from 'dotenv'
dotenv.config()
my .sequelizerc
module.exports = {
'config': path.resolve('config', 'sequelize.js'),
'models-path': path.resolve('src', 'models'),
'seeders-path': path.resolve('src', 'seeders'),
'migrations-path': path.resolve('src', 'migrations')
};
and config/sequelize.js
import dotenv from 'dotenv'
dotenv.config()
export default {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
dialectOptions: {
bigNumberStrings: true
}
},
test: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
},
production: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT,
dialectOptions: {
bigNumberStrings: true,
}
}
};