Node : error Must use import to load ES Module

error message


> tinder-backend@1.0.0 start D:\tinder\tinder-backend
> node server.js

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\tinder\tinder-backend\server.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! tinder-backend@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the tinder-backend@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Advitya\AppData\Roaming\npm-cache\_logs\2021-01-28T09_30_12_760Z-debug.log

package.json

{
  "name": "tinder-backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "esm": "^3.2.25"
  }
}

code

import express  from 'express'
import mongoose from 'mongoose'

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

//middleware
app.get('/',(req,res)=>{
    res.status(200).send("helooo")
})

app.listen(port,()=>console.log(`listening to port ${port}`))
//db config

//api endpoint

//listener

So have you followed the instructions for the esm package? It looks like you are doing neither of the two options given for enabling es standard modules

I dont know about esm

You’ve installed a package called esm: https://github.com/standard-things/esm

It lets you use standard es modules rather than common js. If you don’t understand that, then change the import x from "y" to normal Node require style imports

1 Like