Home > Software design >  Cannot import router from file with ES6 import/export?
Cannot import router from file with ES6 import/export?

Time:07-19

I am converting my whole node app to use the ES6 syntax import NME from "./file"

In my app.js file I am trying to import my routers but am getting the error "Cannot find module '/Users/app/git/app-node-api/src/routers/availability' imported from /Users/app/git/app-node-api/src/app.js"

This was working perfectly fine before using the require statements but since I am new to using the import for modules, something has messed up.

Why is this not working? I tried different export syntax's like export { router } but it didn't work?

My current import/export from files looks like below...

APP.JS

import express from "express"
import("./db/mongoose")
const app = express()

import availabilityRouter from "./routers/availability"
// ^ CANNOT FIND MODULE "/Users/app/git/app-node-api/src/routers/availability" ?

module.exports = app 

AVAILABILITY.JS

import express from "express"
const router = new express.Router()

router.get(`/verify/email/:email`, async (req, res) => {
  // DO STUFF
})

router.get(`/verify/username/:username`, async (req, res) => {
  // DO STUFF
})

module.exports = router // AM I EXPORTING CORRECTLY??

PACKAGE.JSON

{
  "name": "app-api",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon src/index.js",
    "dev": "env-cmd -f ./config/dev.env nodemon src/index.js -e js,hbs",
    "prod": "env-cmd -f ./config/prod.env nodemon src/index.js",
    "test": "env-cmd -f ./config/test.env nodemon --exec 'mocha -R min'",
    "deploy": "make this auto deploy code to ec2 instance"
  },
  "license": "ISC",
  "dependencies": {
   ////
}
}

CodePudding user response:

module.exports commonjs syntax.

try this for ES6

export default router;
  • Related