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

7 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

2 Likes

Thank you, that’s the best response, saved a lot of time!

Please clear this, which file and where i edit this?

1 Like

Hardcoding the values worked for me

Thnak you so much :pray: I think you mean change the scope NOT the key !
This worked for me :slight_smile:

image

I am a total newbie here running into the same problem, where can I find that file to modify the scope/env into run and build time ?

Hi same question here, where do you put this (service: -build_command, etc.) written? Or where do you “Hardcod the values” ?
Thx!

thank you very much ! lol i spent a few nights trying to figure out the issue, and after updating the do app settings for db url to RUN_AND_BUILD_TIME my deployment built successfully :sob: much appreciated

if you are on Digital Ocean App platform

this is in the app spec settings in your settings of the app

sorry as a new user only allowed 1 embedded message at a time