atlassian / better-ajv-errors

JSON Schema validation for Human 👨‍🎤
https://atlassian.github.io/better-ajv-errors/
Other
234 stars 46 forks source link

Import fails for ESM #132

Closed astephens25 closed 2 years ago

astephens25 commented 2 years ago

I am unable to use better-ajv-errors >= 1.0.0 from an ESM project. I have attempted the import on Node 12 and 16 and both fail in the same manner. I checked and the "type" property is not set in the package.json which causes the initial error, but even when set, the import fails due to the lack of file extensions on the imports in the "esm" directory of the module.

I use ESM import exclusively in the project I am working on and have not encountered this with another NPM Module.

(node:41937) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

/Users/aaron/Scitara/Projects/services/node_modules/better-ajv-errors/lib/esm/index.js:1
import { parse } from "@humanwhocodes/momoa";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/Users/aaron/Scitara/Projects/services/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/Users/aaron/Scitara/Projects/services/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at async Object.exports.loadFilesAsync (/Users/aaron/Scitara/Projects/services/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
    at async singleRun (/Users/aaron/Scitara/Projects/services/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/Users/aaron/Scitara/Projects/services/node_modules/mocha/lib/cli/run.js:374:5)
torifat commented 2 years ago

Hmm, there are a few other things wrong with the esm build. I will release a fix soon. Thanks for reporting.

astephens25 commented 2 years ago

You are welcome. If I can assist in any way, please let me know. Otherwise I will watch out for the update.

torifat commented 2 years ago

@astephens25 Please give better-ajv-errors@1.1.2 a try. It should be fixed now.

astephens25 commented 2 years ago

It appears to be working. I have not checked it directly, but a member of my team was able to update to the latest and the project appears to be working. Thanks so much for that.