Harrison Ofordu
Harrison Ofordu

Reputation: 23

How Do solve the following Node Typescript Error

/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)

Upvotes: 0

Views: 1567

Answers (1)

hasezoey
hasezoey

Reputation: 1096

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).

Upvotes: 0

Related Questions