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

204
Views
Import related errors- express js

I am trying to use "module" type javascript with express-js first time. I am trying to create a book tracking application, but I am getting some error when I try to import routes in the main js file, it says cannot find module.

Directory structure:
enter image description here

app.js:

import dotenv from "dotenv";
dotenv.config();

import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import mongoose from "mongoose";

// import routes
import authRoutes from "./routes/auth";

// db config
mongoose
  .connect(process.env.DB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("DB Connected"))
  .catch((err) => console.log(err));
// constants
const app = express();
const port = process.env.PORT || 5001;

// middlewares
app.use(bodyParser.json());
app.use(cors());

// using routes
app.use("/api", authRoutes);

app.listen(port, () =>
  console.log(`Server is running on http://localhost:${port}/`)
);

routes/auth.js:

import express from "express";

const router = express.Router();

router.get("/auth", (req, res) => {
  res.send("Hello from auth");
});

export default router;

package.json:

{
  "name": "server",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT",
  "type": "module",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "mongoose": "^6.0.5"
  }
}

error: enter image description here

Note: Other imports related to database models are all working fine!

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

0

Add js extension Node ESM does not work without it

import authRoutes from "./routes/auth.js";
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!