Could not load js config file config/database.js: Cannot read property ' charAt' of undefined

System Information
  • Strapi Version: v3.3.4
  • Operating System: MacOS Big Sur
  • Database: PostgreSQL
  • Node Version: v14.15.1
  • NPM Version: v6.14.8
  • Yarn Version: N/A

When installing the strapi blog template locally according to the most recent documentation everything works great. I can configure Heroku with a PostgreSQL database and I can add content types locally which get reflected on the production build… but if I add content (like a new Category or Article) to the local server, it isn’t reflected on the production build. I tried to point the local development server at the PostgreSQL db by mirroring the ./config/env/production/database.js in ./config/database.js but I end up with this error:

Could not load js config file config/database.js: Cannot read property ' charAt' of undefined

I’m new to strapi, so I don’t fully understand how data is stored, but it seems like content-types are stored in the build itself, while content is stored in the database (SQLite locally and PostgreSQL in production), and that is where the discrepancy lies.

So the question/issue: Is there a way to a.) get the content to be in sync locally and in production, and b.) do it without producing that error above? This isn’t critical obviously as the application can still fully function if I just revert to using sqlite for local development, but it feels off to me that I would be creating a CMS that has out-of-sync content depending on where I am editing things (Heroku v local).

Code Snippets:
./config/database.js:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/data.db'),
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});

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

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);

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,
      },
      options: {
        ssl: false,
      },
    },
  },
});

That’s an error from pg-connection-string. Have you defined the DATABASE_URL from production in .env?

Hello i have the same error. i follow every step for digital ocean hosting (tutorial from strapi website) and i have this error. how can i fix it please??

3 Likes

Same issue. Did you manage to solve it?

I had the same issue. In Heroku, I changed the Config Vars under Settings. The database string was saved under the name HEROKU_POSTGRESQL_PINK_URL on mine, so I needed to update that to match what was in the config, eg. process.env.DATABASE_URL.

Hope this helps.

The problem here is the ENV variable DATABASE_URL is not available during build time. You need to configure your App spec on DO and set the key to RUN_AND_BUILD_TIME

Bildschirmfoto 2021-10-10 um 17.53.13

2 Likes

@akramaskar You saved my life maaaaan! :blue_heart:

1 Like

Yes, it’s a true answer to this problem.
Also, I researched that when you configured and attached the database, DATABASE_URL is automatically attached to the env file and only available during run time.
Then I deleted DATABASE_URL from envs.
And then manually I added the DATABASE_URL variable to envs again. So there will be RUN_AND_BUILD_TIME

1 Like