admin管理员组

文章数量:1323180

Uncaught Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ./assets/js/jquery/interview.js

I get this error when I try to export a function with module.exports and when I import it inside another file. I tried to use export default but the JS file that calls the original file can't find any method. I also found this topic but I don't have a babel.config.json file...

Here is my code :

Interview.prototype = function ($) {
    const collapseLocalStoredElements = function (localStorageId) {
        let collapseItemsIds = localStorage.getItem(localStorageId);
        console.log(collapseItemsIds)
        }
   

    return {
        collapseLocalStoredElements: collapseLocalStoredElements,
    };
}(jQuery);

// export default Interview;
module.exports = Interview;

App.js

//Import
let InterviewImport = require ('./js/jquery/interview');

//Uses
 let Interview = new InterviewImport();
console.log(Interview.collapseLocalStoredElements('collapseItemsIds'));

Thanks!!

Uncaught Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: ./assets/js/jquery/interview.js

I get this error when I try to export a function with module.exports and when I import it inside another file. I tried to use export default but the JS file that calls the original file can't find any method. I also found this topic https://github./babel/babel/issues/12731 but I don't have a babel.config.json file...

Here is my code :

Interview.prototype = function ($) {
    const collapseLocalStoredElements = function (localStorageId) {
        let collapseItemsIds = localStorage.getItem(localStorageId);
        console.log(collapseItemsIds)
        }
   

    return {
        collapseLocalStoredElements: collapseLocalStoredElements,
    };
}(jQuery);

// export default Interview;
module.exports = Interview;

App.js

//Import
let InterviewImport = require ('./js/jquery/interview');

//Uses
 let Interview = new InterviewImport();
console.log(Interview.collapseLocalStoredElements('collapseItemsIds'));

Thanks!!

Share Improve this question edited Oct 21, 2022 at 9:51 MrsStorm asked Oct 21, 2022 at 9:25 MrsStormMrsStorm 551 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Try using ESM syntax.

export default Interview;

App.js

Import Interview from ('./js/jquery/interview');

There also seem to be one too many closing brackets in your code, I've removed the one that I think was unnecessary. The function also seems to be self-executing. You might want to remove that.

Interview.prototype = function (localStorageId) {
  let collapseItemsIds = localStorage.getItem(localStorageId);
  console.log(collapseItemsIds);
};

Happened to me because I was importing something as ES module and exporting everything in CommonJS style in the same file.

本文标签: