Trying to get postgre with an api, getting an error

Its my first time using neon, I’ve always used Render before but it’s rather slow so I wanted to try Neon. This is the error I’m getting

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

Error: Cannot find module './api'
Require stack:
- /Users/jpiesco/Desktop/Coding_projects/Discord_bots/quackBot/coding_quiz_bot_be/app.js

I’m pretty sure the error is coming from here:

const postgres = require('postgres');
require('dotenv').config();

const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;
const URL = `postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}?options=project%3D${ENDPOINT_ID}`;

const sql = postgres(URL, { ssl: 'require' });

async function getPostgresVersion() {
  const result = await sql`select version()`;
  console.log(result);
}

getPostgresVersion();
const express =require('express');
const server = express();
const apiRouter = require('./api');
const {client}= require('./db/client');
client.connect();

const morgan= require('morgan');
server.use(morgan('dev'));
const cors =require('cors');
server.use(cors());
server.use(express.json());

server.use((req,res, next)=>{
  console.log("<___Body Logger START____>");
  console.log(req.method);
  console.log("____Body Logger END____>");
  next();
})

server.use('/api', apiRouter);

server.listen(PORT,()=>{
  console.log('The server is up on port', PORT)
});

I’ve tried defining a port like I would normally but it doesn’t like that either. I set my backend up in a db folder with db functions then an api folder with api functions. It is possiable the error is coming from here as well. Can anyone point me in the right direction?

const express = require("express");
const apiRouter = express.Router();

apiRouter.use("/", async (req, res, next) => {
  console.log("Score that quiz");
  next();
});
apiRouter.get("/", (req, res, next) => {
  console.log("A get request was made to /api");
  res.send({ message: "success" });
});
const usersRouter = require("./users");
apiRouter.use("/users", usersRouter);

module.exports = apiRouter;

Hey @JessyPiesco :wave: Welcome!

I’d love to help. Can you share an example repo?

I assume that in your project, you have an index.js file and an api.js file. Is that correct?

Normally I just have an index.js but when I was reading neon it looked like they did everything with the label app.js, so I switched it. Is it something simple where I accidentally typed index.js?
Here’s my git hub repo

Could you do the following:

  1. Renaming the app.js that is in the root of your project to index.js
  2. Modifying the import to here from ./api to to ./api/app.js? If you don’t want to change the import then you can rename the file located at api/app.js to index.js

This should fix the issue. Let me know if you’re still getting an error

ok that got rid of that error now I’m getting

The server is up on port 8080
/Users/jpiesco/Desktop/Coding_projects/Discord_bots/quackBot/coding_quiz_bot_be/node_modules/pg-protocol/dist/parser.js:287
        const message = name === 'notice' ? new messages_1.NoticeMessage(length, messageValue) : new messages_1.DatabaseError(messageValue, length, name);
                                                                                                 ^

error: role "JessyPiesco" does not exist

JessyPiesco is the pguser the docs had me set up from making the project in neon

If you go to the roles page (projects > your project > roles), do you have a role with the name JessyPiesco? Otherwise you will need to create it

Yes, I just doubled checked and it def has that role under Roles.

Are you using the Neon connection string during local dev? i saw this part in the code. coding-bot-be/client.js at 547e925d7ef628e6b28cfaf3d46b78802c2b1ed3 · Quackathon-Quiz-Bot/coding-bot-be · GitHub

Also is there a reason that you are creating another client in the app.js file? This portion is not necessary since you have the db/client.ts part. coding-bot-be/app.js at 547e925d7ef628e6b28cfaf3d46b78802c2b1ed3 · Quackathon-Quiz-Bot/coding-bot-be · GitHub

OK so with that knowledge I redid some file to make that make more sense but now it’s not letting me do client.connect() I’ve tried putting it into multiple spots but it won’t do it. I’ve console logged client to make sure it importing correctly and it is but it says client.connect is not a function. Any ideas?

Error dropping tables
Error during building database
TypeError: client.connect is not a function
    at dropTables (/Users/jpiesco/Desktop/Coding_projects/Discord_bots/quackBot/coding_quiz_bot_be/db/seed.js:8:18)
    at buildingDB (/Users/jpiesco/Desktop/Coding_projects/Discord_bots/quackBot/coding_quiz_bot_be/db/seed.js:43:11)```

Does the repo you shared has the latest code?

yes I pushed it to the git hub

you’re importing client incorrectly. It should be wrapped in curly braces.

So it should be

const  {client}  = require("./client");

I ran the seed script and it worked. If you run into any Neon-related issues let me know

1 Like

Aww well now I feel silly. Thank you for your help though!

No problem! Glad I can help :smile:

1 Like