Fonsecar
Fonsecar

Reputation: 21

ESmodules: The requested module '****' does not provide an export named '****'

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!

Upvotes: 1

Views: 3783

Answers (1)

geeky01adarsh
geeky01adarsh

Reputation: 400

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;

Upvotes: 0

Related Questions