Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
why is Requiring mongoose from local file is not working?

So I have the code

Directory/app/script.js:-

// MongoDb
// Connect to MongoDB through Mongoose
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27012/profiles')

// Requiring the Schemas
const SignIn = require('MongoDb/Models/profiles.js')

console.log(SignIn)

So what is wrong in the code. The console in the web says that require is not defined. Can you help me with how to go about it.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are two reasons you might get this error. Since you said

The console in the web says

I expect it is the first reason in your code

You are running the code in a web browser

Node.js is a stand-alone environment for running JavaScript. It is not a browser extension.

To run Node.js code, save it in a file with a .js file extension, then run it with:

node yourFile.js

If you want Node.js code and browser code to interact then the typical way is to write a server in Node.js that hosts a web service. Express.js is a popular way to do this. You can then use Ajax to make HTTP requests to that web service.

For bi-directional communication (as opposed to the request/response of Ajax) look at Socket.io.

You are using ECMAScript modules

Node originally used CommonJS modules which use module.exports and require.

Recent versions also support ECMAScript modules which use export and import.

Either switch to using import instead of require or change your configuration to use CommonJS modules.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!