I'm trying to connect to my MongoDB database and I'm getting this error
ReferenceError: require is not defined
at file:///Users/admin/mjml/mjml/playground.js:1:21
at ModuleJob.run (node:internal/modules/esm/module_job:146:23)
at async Loader.import (node:internal/modules/esm/loader:165:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
const MongoClient = require('mongodb').MongoClient
const uri =
'------------------------------'
const client = new MongoClient(uri, { useNewUrlParser: true })
client.connect((err) => {
const collection = client.db('test').collection('devices')
// perform actions on the collection object
client.close()
})
You are attempting to use require() inside an ESM module (you can see the Object.loadESM in the call stack of the error) which tells us it's an ESM module. You can't use require() in that type of module. Instead, you must use import.
So, you probably want:
import {MongoClient} from "mongodb";
Related
I just connected mongoDB using mongoose.
But I got error TypeError: Cannot read properties of undefined (reading 'split')
How can I fix this error?
Here's my code
export const dbConnect = async () => {
mongoose.connect(process.env.NEXT_PUBLIC_MONGO_URI);
const db = mongoose.connection;
db.on('error', function () {
console.log('db connection failed!');
});
db.once('open', function () {
console.log('db connected!');
});
};
And I am using mongoose version 6.5.3, next version 12.2.5
If the error appears in the browser, it means that you are trying to use Mongoose in your client side code.
In fact, somewhere in its code, Mongoose checks for the version of the node installation it's using.
Being ran in the browser, there is no such thing as process.versions.node, hence the error.
What is value of process.env.NEXT_PUBLIC_MONGO_URI
Format should be like
mongoose.connect('mongodb://localhost/myapp');
mongoose.connect('mongodb://username:password#host:port/database?options...');
I think the problem will be in the connect function. The URI you give in might be wrong. Try logging it our before the function, to make sure it's a correct uri.
https://www.mongodb.com/docs/manual/reference/connection-string/
Here you can find the correct connection string format.
TL;DR
Use a dynamic import. instead of:
import mongoose from 'mongoose';
export const connectDb = async () => {
try {
await mongoose.connect('your-connection-string', { });
} catch (error) {
// exit process with failure
process.exit(1);
}
};
Try:
import dynamic from 'next/dynamic';
// import mongoose from 'mongoose';
export const connectDb = async () => {
try {
// Dynamically load mongoose
const mongoose = (await import('mongoose')).default;
await mongoose.connect('your-connection-string', { });
} catch (error) {
// exit process with failure
process.exit(1);
}
};
See the docs here: Dynamic Imports
Why does this work?
Luca was correct to say the following:
If the error appears in the browser, it means that you are trying to
use Mongoose in your client side code.
In fact, somewhere in its code, Mongoose checks for the version of the
node installation it's using.
To expand on that, node.js is not available on the client side of a next.js project, but it is available where server-side code runs such as getServerSideProps or /pages/api. I assume you only want to use this function inside the /pages/api folder and in that case it should work just fine.
I couldn't find any resources online explaining why I'm running into this issue, even the official Node JS docs / API reference say that createWriteStream is a method in fs:
fs.createWriteStream(path[, options])[src]#
All other stack overflow questions I found were people who were accidentally trying to access fs from the browser. So just to clarify, I am using Node!!
Example Code:
import fs from 'fs/promises'
let ws = fs.createWriteStream("example.png")
Example code output:
file:///workspace/buz/src/error.js:3
let ws = fs.createWriteStream("example.png")
^
TypeError: fs.createWriteStream is not a function
at file:///workspace/buz/src/error.js:3:13
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
Misc:
Node version: v16.15.1
OS: Mac OS
I figured it out:
createWriteStream() is a function of fs, but not of fs/promises!!
I couldn't find ANY documentation online for how to use createWriteStream() with fs promises, so the easiest option is just to import it from the normal callback based fs:
import fs from 'fs/promises'
import {createWriteStream} from 'fs'
let ws = fs.createWriteStream("example.png")
I read through the Node documentation on fs and went through all the methods in the the fs/promises API and none of them return a <WriteStream>. So as of today, it seems like there's no way to get around this issue without importing stuff from the normal fs API.
Update: Changed wording to not use the word synchronous when describing the callback based fs API
if you want use fs/promise. First, you should open file to get a FileHandle. then you can use fileHanlde.createWriteStream().
import fs from 'fs/promises'
let file;
try{
file = await fs.open("example.png");
let ws = file.createWriteStream();
} finally(){
await file?.close();
}
I'm learning express and running the code from the terminal gives me errors, below are my code and the terminal error prompt
const express = require("express");
const app = express();
app.listen(3000);
Terminal said:
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 'C:\Users\ENIOLA YUSUFF\desktop\my-express-server\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///C:/Users/ENIOLA%20YUSUFF/desktop/my-express-server/server.js:5:17
←[90m at ModuleJob.run (node:internal/modules/esm/module_job:197:25)←[39m
at async Promise.all (index 0)
←[90m at async ESMLoader.import (node:internal/modules/esm/loader:337:24)←[39m
←[90m at async loadESM (node:internal/process/esm_loader:88:5)←[39m
←[90m at async handleMainPromise (node:internal/modules/run_main:61:12)←[39m
I tried using this code beneath instead thinking it was the difference in the node version but it solved half the problem.
import * as express from "express";
const app = express();
app.listen(3000);
Terminal error:
$ node server.js
file:///C:/Users/ENIOLA%20YUSUFF/desktop/my-express-server/server.js:2
const app = express();
^
TypeError: express is not a function
at file:///C:/Users/ENIOLA%20YUSUFF/desktop/my-express-server/server.js:2:13
at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
You probably have "type": "module" in the file. That's why you have to use import instead of require.
You should be able to use import express from "express" for it to work.
Try this:
import express from 'express';
const app = express();
In Next.js 12, when I tried to connect mongodb in middleware function in _middleware.js under /pages/api/user folder, I got this error:
TypeError: mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect is not a function
at dbConnect (webpack-internal:///./src/server/utils/dbConnect.js:39:74)
at Object.middleware [as handler] (webpack-internal:///./src/pages/api/user/_middleware.js:19:82)
at adapter (webpack-internal:///./node_modules/next/dist/server/web/adapter.js:30:35)
part of my connection code like this:
import mongoose from "mongoose";
...
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose;
});
So I guess that Accessing Database in _middleware.js is prohibited. Am I right?
I am trying to set up a Postgres database in a nodejs server using ES6 syntax, but I don't think I'm importing and initializing pg-promise properly. If I were using common js sytax I would do the below:
// Create Database Connection
const pgp = require('pg-promise')({});
const db = pgp(config.db);
// Test connection
db.connect()
.then((obj) => {
console.log('Connected to database');
obj.done(); // success, release connection;
})
.catch((error) => {
console.error('ERROR:', error.message);
});
Using ES6 I am trying to do the below but the connection just hangs and doesn't complete or error out.
import pgPromise from 'pg-promise';
// Create Database Connection
const pgp = pgPromise({});
const db = pgp(config.db);
// Test connection
db.connect()
.then((obj) => {
console.log('Connected to database');
obj.done(); // success, release connection;
})
.catch((error) => {
console.error('ERROR:', error.message);
});
I've searched through the pg-promise docs and can't find anything about using it with ES6 syntax. Any ideas on what I should change?
The correct as I can see in the documentation.
Loading and initializing the library with Initialization Options:
const initOptions = {/* initialization options */};
const pgp = require('pg-promise')(initOptions);
or without Initialization Options:
const pgp = require('pg-promise')();
Create your Database object from the connection as pgp(connection, [dc]):
const db = pgp(connection);
For ES6 or TypeScript syntax
import pgPromise from 'pg-promise';
const pgp = pgPromise({/* Initialization Options */});
const db = pgp('postgres://username:password#host:port/database');
Is there any error message? Nodejs needs specific conditions to support es module, first make sure you have introduced the module correctly.
// index.mjs
import pgPromise from 'pg-promise';
const pgp = pgPromise({});
console.log(pgp);
Then execute with a --experimental-modules
node --experimental-modules index.mjs
More details https://blog.logrocket.com/es-modules-in-node-js-12-from-experimental-to-release/
Alright this is pretty stupid, but I found out my problem was just that I needed to update the pg-promise dependency. I was using version 8.5.1 and upgrading to 10.5.7 fixed this issue. For anyone else running into this issue you can use the code for ES6 as written in the question just make sure your pg-promise dependency is at the latest version.