gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.42k stars 512 forks source link

Mocha ESM Error #306

Closed smiles3983 closed 2 years ago

smiles3983 commented 2 years ago

I can make my app itself work, but when I try to run Mocha for unit testing I cant get past the ESM error:

Error [ERR_REQUIRE_ESM]: require() of ES Module c:\ws\Insight\wordaddin\node_modules\@zip.js\zip.js\index.js from c:\ws\Insight\wordaddin\src\main\client\App.jsx not supported. Instead change the require of index.js in c:\ws\Insight\wordaddin\src\main\client\App.jsx to a dynamic import() which is available in all CommonJS modules.

Im using a dynamic import const zip = await import("@zip.js/zip.js");

If I change this to import zip from "@zip.js/zip.js" mocha just ends with no console output at all.

smiles3983 commented 2 years ago

I got it to work. Had to set modules: false in babel

"presets": [
    ["@babel/preset-env",
      {
        "modules": false
      }
    ],
gildas-lormeau commented 2 years ago

I agree with you. I also set modules to false when transpiling zip.js with babel, see below: https://github.com/gildas-lormeau/zip.js/blob/fe572b7ca5e38f819b75e68efe890946bffb90d6/rollup-es5.config.js#L44

DavideMacchia commented 2 years ago

I get a similar error on Node:

internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/davide/WebstormProjects/test-cose/node_modules/@zip.js/zip.js/index.js require() of ES modules is not supported. require() of /home/davide/WebstormProjects/test-cose/node_modules/@zip.js/zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/davide/WebstormProjects/test-cose/node_modules/@zip.js/zip.js/package.json.

I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@zip.js/zip.js": "^2.4.4", "typescript": "^4.5.5" } }

smiles3983 commented 2 years ago

How are you importing it? I had to change it to

Const zip = @.***….

On Sat, Feb 19, 2022 at 6:26 AM Davide Macchia @.***> wrote:

I get a similar error on Node:

internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: @./zip.js/index.js require() of ES modules is not supported. require() of @./zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from @.***/zip.js/package.json.

I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { @.***/zip.js": "^2.4.4", "typescript": "^4.5.5" } }

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1045995545, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQENR7FWRHX46RELA44TU35465ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

I have tried both ways: import * as zip from "@zip.js/zip.js"; const zip = require("@zip.js/zip.js"); and I got the same error.

smiles3983 commented 2 years ago

Change require to import.

On Sat, Feb 19, 2022 at 7:18 AM Davide Macchia @.***> wrote:

How are you importing it? I had to change it to Const zip = @.

…. … <#m4753957838317275719> On Sat, Feb 19, 2022 at 6:26 AM Davide Macchia @.> wrote: I get a similar error on Node: internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: @./zip.js/index.js require() of ES modules is not supported. require() of @./zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from @./zip.js/package.json. I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { @./zip.js": "^2.4.4", "typescript": "^4.5.5" } } — Reply to this email directly, view it on GitHub <#306 (comment) https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1045995545>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQENR7FWRHX46RELA44TU35465ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @ .***>

I have tried both ways: import * as zip from @./zip.js"; const zip = @./zip.js"); and I got the same error.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046005320, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEIMH3D7FXSOXNGOP2DU36DCDANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

smiles3983 commented 2 years ago

const zip = @.***/zip.js");

On Sat, Feb 19, 2022 at 9:24 AM James @.***> wrote:

Change require to import.

On Sat, Feb 19, 2022 at 7:18 AM Davide Macchia @.***> wrote:

How are you importing it? I had to change it to Const zip = @.

…. … <#m_-7632006095177696315_m4753957838317275719> On Sat, Feb 19, 2022 at 6:26 AM Davide Macchia @.> wrote: I get a similar error on Node: internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: @./zip.js/index.js require() of ES modules is not supported. require() of @./zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from @./zip.js/package.json. I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { @./zip.js": "^2.4.4", "typescript": "^4.5.5" } } — Reply to this email directly, view it on GitHub <#306 (comment) https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1045995545>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQENR7FWRHX46RELA44TU35465ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @ .***>

I have tried both ways: import * as zip from @./zip.js"; const zip = @./zip.js"); and I got the same error.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046005320, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEIMH3D7FXSOXNGOP2DU36DCDANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

smiles3983 commented 2 years ago

But if you do it this way, it’s a promise. So you’ll have to put an await statement before it or handle its return. What I did was move it to the function I need it in and made the function async. So then it would be

const zip = await @.***/zip.js");

On Sat, Feb 19, 2022 at 9:24 AM James @.***> wrote:

const zip = @.***/zip.js");

On Sat, Feb 19, 2022 at 9:24 AM James @.***> wrote:

Change require to import.

On Sat, Feb 19, 2022 at 7:18 AM Davide Macchia @.***> wrote:

How are you importing it? I had to change it to Const zip = @.

…. … <#m_6285021164806850285m-7632006095177696315_m4753957838317275719> On Sat, Feb 19, 2022 at 6:26 AM Davide Macchia @.> wrote: I get a similar error on Node: internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: @./zip.js/index.js require() of ES modules is not supported. require() of @./zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from @./zip.js/package.json. I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { @./zip.js": "^2.4.4", "typescript": "^4.5.5" } } — Reply to this email directly, view it on GitHub <#306 (comment) https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1045995545>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQENR7FWRHX46RELA44TU35465ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @ .***>

I have tried both ways: import * as zip from @./zip.js"; const zip = @./zip.js"); and I got the same error.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046005320, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEIMH3D7FXSOXNGOP2DU36DCDANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

smiles3983 commented 2 years ago

I mean

const zip = await @.***/zip.js");

On Sat, Feb 19, 2022 at 9:26 AM James @.***> wrote:

But if you do it this way, it’s a promise. So you’ll have to put an await statement before it or handle its return. What I did was move it to the function I need it in and made the function async. So then it would be

const zip = await @.***/zip.js");

On Sat, Feb 19, 2022 at 9:24 AM James @.***> wrote:

const zip = @.***/zip.js");

On Sat, Feb 19, 2022 at 9:24 AM James @.***> wrote:

Change require to import.

On Sat, Feb 19, 2022 at 7:18 AM Davide Macchia @.***> wrote:

How are you importing it? I had to change it to Const zip = @.

…. … <#m_4762310197143719007_m_6285021164806850285m-7632006095177696315_m4753957838317275719> On Sat, Feb 19, 2022 at 6:26 AM Davide Macchia @.> wrote: I get a similar error on Node: internal/modules/cjs/loader.js:1102 throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); ^ Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: @./zip.js/index.js require() of ES modules is not supported. require() of @./zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from @./zip.js/package.json. I'm testing the library on an empty project and can't find a way to work it out. The package.json is very simple: { "name": "test-cose", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "start": "tsc src/index.ts && node src/index.js", "test": "echo "Error: no test specified" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { @./zip.js": "^2.4.4", "typescript": "^4.5.5" } } — Reply to this email directly, view it on GitHub <#306 (comment) https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1045995545>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQENR7FWRHX46RELA44TU35465ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @ .***>

I have tried both ways: import * as zip from @./zip.js"; const zip = @./zip.js"); and I got the same error.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046005320, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEIMH3D7FXSOXNGOP2DU36DCDANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

I'm not sure why but your message is partially censured so it's not clear what should I write between "await" and the "@"

DavideMacchia commented 2 years ago

if your solution is: "const zip = await import("@zip.js/zip.js");" this doesn't work either and I get the same error.

smiles3983 commented 2 years ago

Yeah that was it. What version of node are you running?

DavideMacchia commented 2 years ago

It's the v14.19.0

smiles3983 commented 2 years ago

Can you upgrade to 16?

DavideMacchia commented 2 years ago

I did and now the error has changed into this: Error [ERR_REQUIRE_ESM]: require() of ES Module /home/davide/WebstormProjects/test-cose/node_modules/@zip.js/zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.ts not supported. Instead change the require of index.js in /home/davide/WebstormProjects/test-cose/src/zipManager.ts to a dynamic import() which is available in all CommonJS modules. at /home/davide/WebstormProjects/test-cose/src/zipManager.ts:36:73 { code: 'ERR_REQUIRE_ESM' }

I'm still using this import: const zip = await import("@zip.js/zip.js");

smiles3983 commented 2 years ago

Did you change the babel setting like my second comment to modules: false?

On Sat, Feb 19, 2022 at 1:24 PM Davide Macchia @.***> wrote:

I did and now the error has changed into this: Error [ERR_REQUIRE_ESM]: require() of ES Module @.***/zip.js/index.js from /home/davide/WebstormProjects/test-cose/src/zipManager.ts not supported. Instead change the require of index.js in /home/davide/WebstormProjects/test-cose/src/zipManager.ts to a dynamic import() which is available in all CommonJS modules. at /home/davide/WebstormProjects/test-cose/src/zipManager.ts:36:73 { code: 'ERR_REQUIRE_ESM' }

I'm still using this import: const zip = await @.***/zip.js");

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046077366, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEKZM2KAX6O4BUSH77DU37N6JANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

I'm not using babel in the project, maybe that's the problem?

smiles3983 commented 2 years ago

Are you doing any React or Angular? If your only running node and not using a browser you shouldn’t need babel.

On Sat, Feb 19, 2022 at 2:13 PM Davide Macchia @.***> wrote:

I'm not using babel in the project, maybe that's the problem?

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046086295, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEN4S5UCPXU2OZS3G5DU37TU5ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

Yeah, just node for the moment. I'm using typescript, not sure if this change something.

smiles3983 commented 2 years ago

It might there were a ton of typescript settings I saw online researching my issue. You might have to dig around about settings for the typescript config file.

On Sat, Feb 19, 2022 at 3:43 PM Davide Macchia @.***> wrote:

Yeah, just node for the moment. I'm using typescript, not sure if this change something.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046099277, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEOCDDGJRMRIYT6MWSDU376GPANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

Yeah I guess so, that's that first thing I have checked tbh.

smiles3983 commented 2 years ago

https://www.typescriptlang.org/docs/handbook/esm-node.html

On Sun, Feb 20, 2022 at 8:06 AM Davide Macchia @.***> wrote:

Yeah I guess so, that's that first thing I have checked tbh.

— Reply to this email directly, view it on GitHub https://github.com/gildas-lormeau/zip.js/issues/306#issuecomment-1046234121, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFLLQEM5QSISHDEO2O6QGGTU4DRO7ANCNFSM5OYH4IZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

DavideMacchia commented 2 years ago

I have opened another specific issues for this, thx for the help btw.