Seeding psql db & connecting to Node js app

I recently deployed a Nodejs app / express server and connected it to a deployed postgresql db; however, I can’t figure out how to seed this db as I normally would in my local environment.

In my local environment, I would run a script to seed my database with default data using my package manager (npm):

The Script:

"db:build": "node ./src/backend/seed.js

Command Executed in Local Environment:

npm run db:build

I was able to connect to the database with the PSQL Command, but that’s as far as I’ve gotten.

Any suggestions?
Thanks for the assist!

Hi there,

Thanks for reaching out.

On a paid plan you could use Shell/SSH or Jobs to run a one-off command.

If you’re using free you may have to workaround it by adding it to you build command, then removing it once it’s been run once, e.g. a build command of

npm install; npm run db:build

Then once the DB is setup run, change the build command back to just npm install; (or if the db:build is idempotent, you could leave it there).

Hope that helps

Alan

1 Like

Thanks for the assist!

The workaround didn’t work - so I’ll upgrade to paid and attempt to use the first possible solution you suggested!

I must be missing something small here.

My package.json has the following structure:

"scripts": {
    "start:react": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "start": "node ./src/express.js",
    "seed:dev": "nodemon ./src/backend/seed.js",
    "start:dev": "nodemon ./src/express.js",
    "db:build": "node ./src/backend/seed.js",
    "test:watch:db": "jest --watchAll --runInBand --verbose db"
  },

And as mentioned above earlier in the thread, I ran:

Build Command:

npm install;

Start Command:

npm start

This gets the web server running. I run into issues when trying to run npm run db:build to seed my psql database, which I connected with the Internal Database URL and saved into my Web Service’s Environment Variables.

When running npm run db:build, I receive the following output:

> turtles_v.1@0.1.0 db:build
> node ./src/backend/seed.js

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '/opt/render/project/src/src/backend/seed.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

As for my project directory:

render@srv-cdcrhh6n6mpsbhf4kvb0-6576d699f9-rdvgk:~/project/src$ ls

NOTES.md  README.md  node_modules  package-lock.json  package.json  public  src

With the next level down being:

render@srv-cdcrhh6n6mpsbhf4kvb0-6576d699f9-rdvgk:~/project/src/src$ ls

README.md  backend  diagrams  express.js  frontend  index.css  index.js  jest  logo.svg  reportWebVitals.js  setupTests.js

Am I missing anything?
Something tells me its miniscule.

Thanks again!

Hi again,

I’d be curious what happened when you say “The workaround didn’t work”, adding a command to the build command should run as expected, unless it threw an error.

You didn’t share the contents of the backend directory, if seed.js does exist, maybe check the casing, as that’s important on Linux.

Kind regards

Alan

2 Likes

You nailed it - it was a casing issue.
In the repository, seed.js was Seed.js.

Once I changed the casing to match my local repo., everything worked as expected.

Thank you for your help! I’m grateful!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.