0
/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/nanoid/index.js from /Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/model/userModel.ts not supported.
Instead change the require of index.js in /Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/model/userModel.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.require.extensions.<computed> [as .js] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/model/userModel.ts:26:18)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/service/userService.ts:7:37)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/controller/userController.ts:13:23)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/routes/userRouter.ts:7:26)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/routes/index.ts:8:38)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/src/app.ts:11:34)
    at Module.m._compile (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/index.js:859:16)
    at phase4 (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/bin.js:466:20)
    at bootstrap (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/bin.js:54:12)
    at main (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/Users/oforduhh/Documents/EXPRESS JS/ts_api_auth/node_modules/ts-node/dist/bin.js:579:5) {
  code: 'ERR_REQUIRE_ESM'
}

I'm following a typescript tutorial https://www.youtube.com/watch?v=qylGaki0JhY&t=357s. can't find my way around this error even though I followed the exact step as the tutorial, This is my last resort

import { Request, Response } from "express";
import UserModel from "../model/userModel";
import { CreateUserInput } from "../schema/userSchema";
import log from "../utils/logger";

export const createUserHandler = async (
  req: Request<{}, {}, CreateUserInput>,
  res: Response
) => {
  const body = req.body;

  try {
    const user = await UserModel.create(body);

    res.json({
      message: "User created",
      status: true,
      data: user,
    });
  } catch (error: any) {
    if (error.code === 11000) {
      return res.status(409).send("Account already exists");
    }

    res.status(500).send("Internal server error");
    log.error(error);
  }
};

That is my controller file. it does not allow me call UserModel.create(body)

  • [Same thing](https://stackoverflow.com/questions/70800567/return-oldm-filename-error-err-require-esm-require-of-es-module) try adding `"type": "module"` to `package.json` – Konrad Aug 28 '22 at 17:45
  • 1
    TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" – Harrison Ofordu Aug 28 '22 at 18:00
  • Just pasting an error doesn't help. You have to describe what you did and then you can paste an error. Also, error messages usually have some stack trace that points to the exact place the error happened. – Konrad Aug 28 '22 at 18:01
  • i understand. i have checked stack overflow and i found out the error is common but no solution yet – Harrison Ofordu Aug 28 '22 at 18:05
  • They are not traces, i copied the entire error on the log. let me describe what i did – Harrison Ofordu Aug 28 '22 at 18:06
  • const user = await UserModel.create(body); i get this error when i try to create a new user in my controller file – Harrison Ofordu Aug 28 '22 at 18:11

1 Answers1

0

From what i can tell from the Error, the problem seems to be you are trying to import nanoid, which is a ESM Module into a CommonJS file with require (typescript compiles it to that with default settings). Which is not possible and you will have to use await import('nanoid')(somewhere inside your async-function and maybe cache it afterwards) somewhere in your code or change your whole project to ESM.

See ECMAScript Modules in Node.js for how to convert your project to ESM - though note that importing CommonJS packages will not always work with named imports (even if the types say it is possible).

hasezoey
  • 998
  • 9
  • 24