javascriptnode.jsecmascript-6mernnodemon

Module not found node_modules\@emailjs\nodejs\mjs\models\EmailJSResponseStatus'


I am trying to use EmailJs to send Emails from my NodeJs backend. I am using ES6 version of JS.

I installed EmailJs using command:

npm i @emailjs/nodejs

After that I checked my nodemodules directory. The @emailjs folder is present.

I am importing this Module in my emailService.js file as:

import emailjs from "@emailjs/nodejs";

export const sendPasswordResetEmail = async (emailData) => {
  try {
    const response = await emailjs.send(
      process.env.EMAIL_SERVICE_ID,
      process.env.EMAIL_TEMPLATE_ID,
      {
        to_email: emailData.receiver,
        subject: "Password Reset",
        template_params: {
          user_email: emailData.receiver,
          message: `Click the following link to reset your password: ${emailData.link}`,
        },
      },
      process.env.EMAIL_USER_ID
    );
    console.log('Email sent successfully:', response);
  } catch (error) {
    console.error('Error sending email:', error);
    throw error;
  }
}


When I try to hit npm start I get error

node:internal/modules/esm/resolve:265
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Code repo\MERN Tutorial Proj\Code-Snippets\server\node_modules\@emailjs\nodejs\mjs\models\EmailJSResponseStatus' imported from C:\Code repo\MERN Tutorial Proj\Code-Snippets\server\node_modules\@emailjs\nodejs\mjs\index.js
    at finalizeResolution (node:internal/modules/esm/resolve:265:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///C:/Code%20repo/MERN%20Tutorial%20Proj/Code-Snippets/server/node_modules/@emailjs/nodejs/mjs/models/EmailJSResponseStatus'
}

For reference I am attaching the index.js of the module:

import { EmailJSResponseStatus } from './models/EmailJSResponseStatus';
import { init } from './methods/init/init';
import { send } from './methods/send/send';
export { init, send, EmailJSResponseStatus };
export default {
    init,
    send,
    EmailJSResponseStatus,
};

this is node_modules@emailjs\nodejs\mjs\models\EmailJSResponseStatus file

export class EmailJSResponseStatus {
    constructor(_status = 0, _text = 'Network Error') {
        this.status = _status;
        this.text = _text;
    }
}

I have tried reinstalling the npm package but it didn't work. I cannot think of any solutions since I am new to NodeJs


Solution

  • NodeJS has two module systems: CommonJS (old, custom built) and ESM (recently adopted standard). This line of the error message suggests that you're using ESM:

    node:internal/modules/esm/resolve:265
        throw new ERR_MODULE_NOT_FOUND(
    

    In ESM, you have to specify the extension of the file you're importing. Try changing the import statements in the source code of the index.js of the module (in node_modules) to this, it should start working:

    import { EmailJSResponseStatus } from './models/EmailJSResponseStatus.js';
    import { init } from './methods/init/init.js';
    import { send } from './methods/send/send.js';
    

    Since you're not in control of this file and shouldn't change it directly (it is copied to node_modules at npm install), I'd say that either:

    This chapter of the documentation suggests that to import a CommonJS module into an ESM application you could use dynamic import expression:

    const emailjs = await import("@emailjs/nodejs");
    
    export const sendPasswordResetEmail = async (emailData) => {
    

    … or create an instance of require() function by yourself and use it:

    import { createRequire } from 'module';
    
    const require = createRequire(import.meta.url);
    const emailjs = require("@emailjs/nodejs");
    
    export const sendPasswordResetEmail = async (emailData) => {