longshotlabs / simpl-schema

A JavaScript schema validation package that supports direct validation of MongoDB update modifier objects
https://www.npmjs.com/package/simpl-schema
MIT License
560 stars 114 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module NodeJS #471

Closed danyhiol closed 1 year ago

danyhiol commented 1 year ago

I am getting the following error when using NodeJS: Error [ERR_REQUIRE_ESM]: require() of ES Module path\node_modules\mongo-object\dist\index.js from path\node_modules\simpl-schema\dist\cjs\clean.js not supported.

This happens when I try to pass SimpleSchema as a class constructor property:

...
import type SimpleSchema from 'simpl-schema'; 
...

class Myclass {
  private simpleschema: typeof SimpleSchema;
  ...
  contructor({ schema: typeof SimpleSchema }){
    this.simpleschema = schema;
  }
  ...
}

somewhere in my code:

...
import SimpleSchema from 'simpl-schema'; 
...

const do = new MyClass({ 
  schema: SimpleSchema // <- here is where the error occurs
}); 
github-actions[bot] commented 1 year ago

Thank you for submitting an issue!

If this is a bug report, please be sure to include, at minimum, example code showing a small schema and any necessary calls with all their arguments, which will reproduce the issue. Even better, you can link to a saved online code editor example, where anyone can immediately run the code and see the issue.

If you are requesting a feature, include a code example of how you imagine it working if it were implemented.

If you need to edit your issue description, click the [...] and choose Edit.

Be patient. This is a free and freely licensed package that I maintain in my spare time. You may get a response in a day, but it could also take a month. If you benefit from this package and would like to see more of my time devoted to it, you can help by sponsoring.

slowdownitsfine commented 1 year ago

same issue here in a meteor project where I use the native mongo driver in parallel to the meteor one.

jtarvainen commented 1 year ago

I also have this issue in a Meteor project.

aldeed commented 1 year ago

In the issue description, it seems like you would not want to do both import type and typeof. Did you try with just import SimpleSchema?

That problem may be unrelated to the error, though. It seems as though all reports are in Meteor apps? Maybe there is some extra config necessary to tell Meteor how to import this properly (commonjs vs ESM)

aldeed commented 1 year ago

While debugging another issue, I think I have more insight into this one. simpl-schema is published with support for either esm or cjs, but mongo-object is esm-only. So this error happens when Node.js tries to go from cjs in simpl-schema to esm in mongo-object. Since I also maintain mongo-object, I'll resolve this by publishing that package with support for both, same as this package.

aldeed commented 1 year ago

Should be fixed in 3.4.1