-2

I'm working on a API integration with the ES6 module system and jQuery. I want to use re-use my addToCart function, that is why I placed it into a separate file. But when calling it I get following error:

Uncaught SyntaxError: Export 'addToCart' is not defined in module

1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Dec 28, 2023 at 13:28

1 Answer 1

0

you should have the latest version of Node.js installed (or, at least 13.2.0+). Then do one of the following, as described in the documentation:

Option 1

In the nearest parent package.json file, add the top-level "type" field with a value of "module". This will ensure that all .js and .mjs files are interpreted as ES modules. You can interpret individual files as CommonJS by using the .cjs extension.

// package.json

{
  "type": "module"
}

Option 2

Explicitly name files with the .mjs extension. All other files, such as .js will be interpreted as CommonJS, which is the default if type is not defined in package.json.

Sign up to request clarification or add additional context in comments.

3 Comments

no I'm using just pure es6 with no pkg.json file that's a problem
then just change the file extension to .mjs
i resolved that it's exporting the var but not async function that's why I used first created the empty object at the top then add the method there and export that moduleExports.addToFavourite = addToFavourite; moduleExports.updateHeaderCounts = updateHeaderCounts; moduleExports.addToCart = addToCart;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.