Essay97
Essay97

Reputation: 874

"Error: Unknown dialect undefined" when deploying Strapi 4.0 app to Heroku

I created a Strapi app using the latest version, the new 4.0 and I wanted to deploy it to Heroku. I did follow the Strapi documentation in order to do so, like explained in this page. Now I'm getting an error that I don't understand, I guess it has something to do with postgres. This is the error

2021-12-18T15:26:26.658380+00:00 app[web.1]: [2021-12-18 15:26:26.656] debug: ⛔️ Server wasn't able to start properly.
2021-12-18T15:26:26.659122+00:00 app[web.1]: [2021-12-18 15:26:26.658] error: Unknow dialect undefined
2021-12-18T15:26:26.659123+00:00 app[web.1]: Error: Unknow dialect undefined
2021-12-18T15:26:26.659123+00:00 app[web.1]: at getDialectClass (/app/node_modules/@strapi/database/lib/dialects/index.js:12:13)
2021-12-18T15:26:26.659123+00:00 app[web.1]: at getDialect (/app/node_modules/@strapi/database/lib/dialects/index.js:19:23)
2021-12-18T15:26:26.659124+00:00 app[web.1]: at new Database (/app/node_modules/@strapi/database/lib/index.js:38:20)
2021-12-18T15:26:26.659124+00:00 app[web.1]: at Function.Database.init (/app/node_modules/@strapi/database/lib/index.js:84:33)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at Strapi.bootstrap (/app/node_modules/@strapi/strapi/lib/Strapi.js:347:30)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at Strapi.load (/app/node_modules/@strapi/strapi/lib/Strapi.js:410:16)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at async Strapi.start (/app/node_modules/@strapi/strapi/lib/Strapi.js:161:9)

Apart from doing what is explained in the docs I linked, I just added a few collections using the UI in development mode. How can I fix this error and deploy to Heroku this new 4.0 version of Strapi?

Upvotes: 15

Views: 18333

Answers (11)

aProgger
aProgger

Reputation: 871

My solution is for deploying Strapi 4 (^4.12) on a root server (ubuntu 22 with plesk obsidian in my case). I know the op asked for heroku, but I stumbled over here many times, finding no answer to my problem. So for others:

After a long journey of anger and despair, I wandered through strapis valley of code and docs and felt no hope. As a last resort, I looked into their core code and found (node_modules/@strapi/strapi/lib/commands/builders/admin.js) the following properties:

module.exports = async ({ buildDestDir, forceBuild = true, optimization, srcDir }) => {
  const strapiInstance = strapi({
    // Directories
    appDir: srcDir,
    distDir: buildDestDir,
    // Options
    autoReload: true,
    serveAdminPanel: false,
  });

  // ...
}

I took my chances and added it to my node startup config:

const strapi = require('@strapi/strapi');
strapi({
  distDir: './dist', // <--- this is important
  autoReload: false,
  serveAdminPanel: true,
}).start();

My Strapi is running now. I am not sure why, but it must have something to do with typescript. Hope it helps and I hope someone can explain why. I am very curious.

Upvotes: 1

Joe Sadoski
Joe Sadoski

Reputation: 676

In my case, I was getting this issue while trying to run tests. I had made some underlying changes, such as install jest, using a jest.config.js, and changes to tsconfig.ts which led to this issue.

I was able to overcome the issue by deleting dist and .cache dirs. A new build was not necessary, since I was using ts-jest.

Upvotes: 0

kadim kareem
kadim kareem

Reputation: 145

it's look like there is a problem in the new strapi version , when i try to create project with typescript support i faced same error , i don't know if you are using typescript in your project but these two solutions solved my problem;

solution (1) :- create compile config ts files with tsc $ tsc .\config\database.ts .\config\server.ts ./ .\config\admin.ts

solution (2) :- downgrade your strapi to 4.3.2 version in the package.json file.

solution (3) :- don't use @latest when creating strapi app instate use 4.3.2 npx [email protected] test-strapi-verion-with-ts --ts

you can use one of theme

Upvotes: -1

Tsquare
Tsquare

Reputation: 21

If you are using typescript in Strapi, we may encounter this issue too as there is a issue affecting Strapi typescript capabilities.

I can resolve the problem when I downgraded Strapi to 4.3.2 from 4.3.8

Upvotes: 1

Drashti Kheni
Drashti Kheni

Reputation: 1140

In strapi v4, you need to use like:

module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host: env("DATABASE_HOST", "localhost"),
      port: env.int("DATABASE_PORT", 5432),
      database: env("DATABASE_NAME", "db-name"),
      user: env("DATABASE_USERNAME", "postgres"),
      password: env("DATABASE_PASSWORD", "password"),
      schema: env("DATABASE_SCHEMA", "public"),
    },
  }
});

Upvotes: 0

Lauris
Lauris

Reputation: 345

Strapi has once again changed the database configuration structure and even their deployment docs use the old and incorrect example.

Here is the most up to date example: Strapi Database Configuration

Upvotes: 0

欧阳维杰
欧阳维杰

Reputation: 1768

In case someone meets the same problem as me.

In my situation, I am using vim in windows as my text editor. After editing the "config/database.js" file. It left a "database.js~" file in that directory. Then, this filename causes the same problems: "Error: Unknown dialect undefined".

I guess it's a bug in strapi. After removing that file, everything works!

So, the solutions for me. I add this line to my vim config file:

set backupdir=~/.vimbackup

Then create a directory named '.vimbackup' in "C:/User/[me]/" .

Upvotes: 0

yeMarn
yeMarn

Reputation: 36

I fixed this issue by using database url directly. Not sure why the old way does not work in Strapi v4.

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connection: {
    client: "postgres",
    connection: {
      connectionString: process.env.DATABASE_URL,
      ssl: {
        rejectUnauthorized: false
      }
    },
  }
});

Upvotes: 0

iveedee
iveedee

Reputation: 582

I had a similar issue when I was connecting pg locally and then realised my connection config was incorrect. When I replaced it with v4 template it worked.

path: config/database.js

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connection: {
    client: "postgres",
    connection: {
      host: env("DATABASE_HOST", "localhost"),
      port: env.int("DATABASE_PORT", 5432),
      database: env("DATABASE_NAME", "bank"),
      username: env("DATABASE_USERNAME", "postgres"),
      password: env("DATABASE_PASSWORD", "0000"),
      schema: env("DATABASE_SCHEMA", "public"),
    },
  }
});

And for Heorku as the article suggested:

path: config/env/production/database.js:

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false
      },
    },
    debug: false,
  },
});

PS: make sure you add pg-connection-string to your dependencies before pushing to heroku

Upvotes: 15

Roshan Khandelwal
Roshan Khandelwal

Reputation: 973

getDialectClass - from the error log

const getDialectClass = client => {
  switch (client) {
    case 'postgres':
      return require('./postgresql');
    case 'mysql':
      return require('./mysql');
    case 'sqlite':
      return require('./sqlite');
    default:
      throw new Error(`Unknow dialect ${client}`);
  }
};

where the client is - db.config.connection

So if you have been following previous solutions - which talked about a connections object etc ( like this )

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: config.host,
        port: config.port,
        database: config.database,
        username: config.user,
        password: config.password,
        ssl: {
          rejectUnauthorized: false,
        },
      },
      options: {
        ssl: true,
      },
    },
  },
});

db.config.connection would return undefined. & so it would fail

Configuring the DB with - https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure Works fine

Upvotes: 1

Safwen Derouich
Safwen Derouich

Reputation: 19

You can check this method using heroku/cli from strapi docs here, but it's the same thing, i'm using version 4.0.2, this methods works on older versions V3, i think the docs needs update, most of it is deprecated, for example "fetching only data (example: posts/project etc..) related to the authenticated user who created it", most of resources related to this topic in docs and forum are deprecated solutions that don't work in V4.

For the deployment, i have the same issue, i tried a lot of solutions but it didn't work for me to, but i managed to get the server running like this:

enter image description here

but i got this problem when i visit the "/admin" page:

enter image description here

Strapi is really interesting, let's hope they update the docs soon and someone respond with a solution here.

Upvotes: -1

Related Questions