1

I use node version 16.14.2, and I try to use ES Module in my project. Below my package.json.

{
  "name": "community",
  "version": "0.0.1",
  "description": "community",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.0",
    "express": "^4.17.3",
    "express-async-handler": "^1.2.0",
    "jsonwebtoken": "^8.5.1",
    "mysql": "^2.18.1",
    "mysql2": "^2.3.3",
    "nodemon": "^2.0.15",
    "sequelize": "^6.19.0"
  },
  "devDependencies": {
    "sequelize-cli": "^6.4.1"
  }
}

I use Sequelize for ORM, and I use sequelize-cli to generate model. My model models/userModel.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    static associate(models) {
    }
  }
  User.init({
    email: DataTypes.STRING,
    password: DataTypes.STRING,
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

and this is my controllers/userController.js

import User from '../models/userModel.js'
import asyncHandler from 'express-async-handler'
import generateToken from '../utils/generateToken.js'

const registerUser = asyncHandler(async(req, res) => {
  const { first_name, last_name, email, password } = req.body
  const userExists = await User.findOne({
    where: { email: email },
    order: [ [ 'createdAt', 'DESC' ]],
  });
  res.send('ok', userExists);
})

I have tried this :

a).I use import on my controller

import User from '../models/userModel.js'

But then I get an error

SyntaxError: The requested module '../models/userModel.js' does not provide an export named 'default'

b). So I change my Model to be like this one (use import and export default)

'use strict';
import { Model } from 'sequelize'
const userModel = (sequelize, DataTypes) => {
  class User extends Model {
    static associate(models) {
    }
  }
  User.init({
    email: DataTypes.STRING,
    password: DataTypes.STRING,
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};
export default userModel;

I don't get any error on server, but when I try to fecth data (register user), I get error on my postman.

"message": "User.findOne is not a function",
"stack": "TypeError: User.findOne is not a function\n ....

Thank you.

Franky1790
  • 61
  • 1
  • 6
  • see also [module.createRequire](https://stackoverflow.com/questions/54977743/do-require-resolve-for-es-modules) – milahu Dec 05 '22 at 12:28

3 Answers3

3
import Module from "node:module";

const require = Module.createRequire(import.meta.url);

Here is the require you’re looking for. Use it to import what the ESM import doesn’t support, such as Node.js add-ons (.node).

2

How to use require and esm in 1 project?

Don't. It creates a mess. Use modern module syntax when not working in a legacy project.

I use Sequelize for ORM, and I use sequelize-cli to generate model.

The sequelize-auto command supports a -l esm parameter to generate ES6 ESM modules.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

import-js is a command-line tool that helps in converting CommonJS require statements to ECMAScript Modules (ESM) import statements.

Here we can use require and import feature in same file.

  1. first add this line in package.json file, "type": "commonjs"

  2. then add these line of code in server.js/index.js file

import Module from "node:module";
const require = Module.createRequire(import.meta.url);
  1. then change the extension of server.js file server.mjs. and don’t forget to change server.js extension in package.json file as well :- "main": "server.mjs",

here we go,