0

How do I rewrite module.exports = model('users', userSchema) CJS export to ESM export syntax? How to import it to another file (with ESM syntax)?

This is how I tried to convert this line to ESM export syntax:

// 1
export { model('users', userSchema) }
// 2
model('users', userSchema)
export { model }

And import:

// 1
import { User } from '../file.js'
// 2
import User from '../file.js'
// 3
import * as User from '../file.js'

But nothing works. I end up getting an error:

SyntaxError: The requested module ../file.js does not provide an export named default or User

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • Suggest you start from here: https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm It's not that fresh but you definitely will learn something from here – zerdox Dec 26 '22 at 07:42

1 Answers1

0

I've managed to solve the issue myself:

Before (CJS):

module.exports = model('users', userSchema)
const User = require('../models/user_model.js')

After (ESM):

export default model('users', userSchema)
import User from '../models/user_model.js'
zerdox
  • 830
  • 8
  • 22