1

Newbie question here. I have this problem using ESmodules.

SyntaxError: The requested module './esModules.js' does not provide an export named 'add'.

I do have the "Type": "modules" in my package.json

My code:

// index.js

import add from "./esModules.js";

let num1 = 15;
let num2 = 20;

console.log(add(num1, num2));

// esModules.js

export const add = (num1, num2) => {
  result = num1 + num2;
};

I tried adding "export default add;" which I read somewhere,

I tried using { add } in the import sentence,

I tried without ".js" in the import sentence,

but it doesn´t work either.

Thanks!

EDIT:

Solution: It worked with { add } and adding the "return result" in the add function!

Fonsecar
  • 21
  • 4
  • 1
    `I tried using { add } in the import sentence` - that should work - what may be an issue is that `result` is not declared ... but then, you'd get a different error – Jaromanda X Jun 21 '23 at 06:34
  • Hi @JaromandaX, the problem was that the "return" in the add function was missing! Weird thing... haha – Fonsecar Jun 21 '23 at 06:37

1 Answers1

0

There could be 2 ways to fix this problem out, first make the export default, or add {} while importing it. Make sure to return the result in both the cases, for assistance Case 1:

index.js

import add from "./esModules.js";

let num1 = 15;
let num2 = 20;

console.log(add(num1, num2));

esModules.js

export const add = (num1, num2) => {
  result = num1 + num2;
  return result;
};

Case 1:

index.js

import add from "./esModules.js";

let num1 = 15;
let num2 = 20;

console.log(add(num1, num2));

esModules.js

const add = (num1, num2) => {
  result = num1 + num2;
  return result;
};
export default add;
geeky01adarsh
  • 312
  • 3
  • 14