Using Replit database with NodeJS and es6

Question:
I am trying to use the replit database in my nodeJS project. I am using ES6 and when I try to import the replit DB I get the errors below

Repl link:
Js-Bookie

index.js

const db = require("@replit/database");

console

const db = require("@replit/database");
           ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/home/runner/jsbookie-backend/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///home/runner/jsbookie-backend/index.js:4:12
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)

And I tried using the info to import the database in a way that would work with ES 6 – but I believe the example given is for Python, not js.
index.js

// Import the Replit database
import db from replit/database;
file:///home/runner/jsbookie-backend/index.js:3
import db from replit/database;
               ^^^^^^

SyntaxError: Unexpected identifier
    at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14)

import vs require are weird and annoying. But in this case it looks like you just need to put " around replit/database. Like this:

"replit/database"
// or this
"@replit/database"
2 Likes

Thanks @not-ethan.

// This worked
"@replit/database"

// This did NOT work
"replit/database"
2 Likes

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