3

I'm unable to run a node terminal on my backend server.js file in my class project. I am NOT running this in my browser, I'm trying to run my localhost through my node terminal. I've tried looking through a few articles, I made sure that requirejs was installed, babel is installed, I've tried substituting in "import" rather than require, and to my knowledge, all of the necessary files are installed in my package.json.

When I run nodemon server, or node server, I get the following error message

const express = require('express');
                ^
ReferenceError: require is not defined

This is the code so far.

const express = require('express');
const cors = require('cors');
// const mongoose = require('mongoose');
const mongoose = require('mongoose')
 require('dotenv').config();



const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });const connection = mongoose.connection;connection.once('open', () => {  console.log("MongoDB database connection established successfully");})
const addLocationsRouter = require('./routes/addLocations.models');
const contactsRouter = require('./routes/contacts.models');
const homeRouter = require('../src/components/Home')
const allLocationsRouter = require('../src/components/alllocations')

app.use('/allLocations', allLocationsRouter)
app.use('/Home', homeRouter);
app.use('/addLocations', addLocationsRouter);
app.use('/contacts', contactsRouter);
app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

I've used require in other parts files in my backend folder and it doesn't throw an error. However, in my server.js file, I seemingly can't use the word require

  • Does this answer your question? [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – Akash Dathan Sep 02 '20 at 15:40
  • The OP isn't asking for help making require work on the front-end. He's asking for help making require work on the backend. – David Reke Sep 02 '20 at 15:54
  • This would solve the issue if I was running it from the browser, but I'm trying to run it from the backend and that's where I'm getting an error. – Saeed Shareef Sep 02 '20 at 16:00
  • 1
    Can you share your package.json as well? – Zach Thacker Sep 02 '20 at 16:04
  • For sure, here you go { "name": "backend", "version": "1.0.0", "description": "dumb", "main": "server.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "axios": "^0.20.0", "babel": "^6.23.0", "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", "mongoose": "^5.10.2", "requirejs": "^2.3.6" } } – Saeed Shareef Sep 02 '20 at 17:05

3 Answers3

1

According to this issue, you should remove "type": "module" from your package.json file.

Can you verify that this happens in any directory? By default a .js file shouldn’t be treated as a module. I suspect that you may be in a directory (or nested directory below) where a package.json has a “type” field set to “module”.

@jkrems You are right. The module "type" was causing the issue. Thank you for flagging that out.

Zach Thacker
  • 2,529
  • 1
  • 23
  • 28
1

Ok, I think I have it

uninstall axios, babel, and requirejs. You don't actually use those packages in your back end.

Then remove "type": "module" from your package.json

David Reke
  • 535
  • 1
  • 5
  • 20
0

In addition, you have to ensure that you are requiring dotenv in the backend (in your file that has express), rather than in your frontend javascript files.

Lesley
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 07:38