trouble using express.js

i have a js file that im using express.js code using the nodemailer module. What im trying to do is have a user enter their details in a form and when they press a button i receive details that they entered as an email, but i dont seem to understand what the problem is. in my browser im getting this error
Uncaught ReferenceError: require is not defined
at app.js:1:20
Uncaught ReferenceError: require is not defined
at app.js:1:20
i did some searching and it said the browser cannot understand node.js files or something like that, Does anyone know what the actua problem is
8 Replies
missymae
missymae4mo ago
It looks like an error in the syntax you used to import something in the first line of your app. If you share the first line the problem might be there.
greenleaf02
greenleaf024mo ago
this is the js
const nodemailer = require("nodemailer");
const bodyParser=require('body-parser');
const express=require('express')

const app=express()
app.use('/submit-form',bodyParser.urlencoded({extended:false}))
app.post('/submit-form', (req,res)=>{
const {firstName,lastName, sessionDate, subject}=req.body
const transporter=nodemailer.createTransport(
{
service: 'gmail',
host: "smtp.gmail.email",
port: 587,
secure: false,
auth: {
user: "",
pass: "",
},
}
)
const emailMessage={
from:'',
to:'',
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
}
transporter.sendMail(emailMessage, (error,info)=>{
if(error){
console.log('Error sending email:', error);
res.status(500).send('Error sending email');
}else {
console.log('Email sent:', info.response);
res.send('Email sent successfully');
}
})
})




app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
const nodemailer = require("nodemailer");
const bodyParser=require('body-parser');
const express=require('express')

const app=express()
app.use('/submit-form',bodyParser.urlencoded({extended:false}))
app.post('/submit-form', (req,res)=>{
const {firstName,lastName, sessionDate, subject}=req.body
const transporter=nodemailer.createTransport(
{
service: 'gmail',
host: "smtp.gmail.email",
port: 587,
secure: false,
auth: {
user: "",
pass: "",
},
}
)
const emailMessage={
from:'',
to:'',
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>",
}
transporter.sendMail(emailMessage, (error,info)=>{
if(error){
console.log('Error sending email:', error);
res.status(500).send('Error sending email');
}else {
console.log('Email sent:', info.response);
res.send('Email sent successfully');
}
})
})




app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Aoi
Aoi4mo ago
Can I get your package.json?
greenleaf02
greenleaf024mo ago
{
"name": "jolly-genius",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"

},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.19.2",
"nodemailer": "^6.9.13"
}
}
{
"name": "jolly-genius",
"version": "1.0.0",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"

},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.19.2",
"nodemailer": "^6.9.13"
}
}
.
glutonium
glutonium3mo ago
try using es modules instead check how to use es modules in nodejs
Aoi
Aoi3mo ago
In your original question, I am confused about what you mean by error in your browser. You are supposed to use node in a terminal to run this script and not a browser Browser doesn't support require statements
AtulAO
AtulAO3mo ago
Hi do you mind sharing your front-end code for this page.
Ken
Ken3mo ago
How are you executing js file? What is its name? Your package.json file hints at a file named app.js If so, in your terminal (not a web browser) either of the following should start your express js server: node app.js …OR… npm run start if you get a “module not found “ error. Trying running’npm install’ first Once the server is running , then you can invoke your web server endpoint POST http://localhost:3000/aubmit-form