[GH-ISSUE #477] Using ES modules in custom rules #391

Closed
opened 2026-03-03 01:26:30 +03:00 by kerem · 14 comments
Owner

Originally created by @pxl9588 on GitHub (Jan 10, 2022).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/477

Is it possible to use ES module in custom rules? I’m running into this error when I use const urlExist = require(‘url-exist’);:

Must use import to load ES module

When I use import urlExist from ‘url-exist’ I get

Cannot use import statement outside of a module

When I change the custom rule to a .mjs I get:

Property ‘names’ of custom rule at index 0 is incorrect

I believe that happens because the rules are imported by markdowlint by require.

Is there a work around for this?

Originally created by @pxl9588 on GitHub (Jan 10, 2022). Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/477 Is it possible to use ES module in custom rules? I’m running into this error when I use `const urlExist = require(‘url-exist’);`: > Must use import to load ES module When I use `import urlExist from ‘url-exist’` I get > Cannot use import statement outside of a module When I change the custom rule to a .mjs I get: > Property ‘names’ of custom rule at index 0 is incorrect I believe that happens because the rules are imported by markdowlint by `require`. Is there a work around for this?
kerem 2026-03-03 01:26:30 +03:00
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

This is not supported currently for the reasons you outline. It may be as simple as changing from require to import, but my other experiences incorporating ESM suggest otherwise. I'll have a look at this for the next round of updates, thanks!

<!-- gh-comment-id:1009305351 --> @DavidAnson commented on GitHub (Jan 10, 2022): This is not supported currently for the reasons you outline. It may be as simple as changing from require to import, but my other experiences incorporating ESM suggest otherwise. I'll have a look at this for the next round of updates, thanks!
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

To clarify, while your rule needs to be CommonJS, it should be able to use an import expression to pull in an ESM dependency. Because that is an asynchronous operation, it would need to take advantage of the new support for asynchronous custom rules. (Which is only available in the library for now; I am rolling it out to CLI2 and the VS Code extension soon.)

<!-- gh-comment-id:1009309800 --> @DavidAnson commented on GitHub (Jan 10, 2022): To clarify, while your rule needs to be CommonJS, it should be able to use an import expression to pull in an ESM dependency. Because that is an asynchronous operation, it would need to take advantage of the new support for asynchronous custom rules. (Which is only available in the library for now; I am rolling it out to CLI2 and the VS Code extension soon.)
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

Which is what you asked about, so yes, I think that's the workaround! :)

<!-- gh-comment-id:1009311115 --> @DavidAnson commented on GitHub (Jan 10, 2022): Which is what you asked about, so yes, I think that's the workaround! :)
Author
Owner

@pxl9588 commented on GitHub (Jan 10, 2022):

Sorry I’m advance,I’m not very familiar with this.

As far as I understand I can’t use import in a CommonJS file because it needs to be a module. So right now I don’t have a way to use the url-exist package. Did you mean there currently is a workaround or that it will be available in future releases?

On Jan 10, 2022, at 3:21 PM, David Anson @.***> wrote:


To clarify, while your rule needs to be CommonJS, it should be able to use an import expression to pull in an ESM dependency. Because that is an asynchronous operation, it would need to take advantage of the new support for asynchronous custom rules. (Which is only available in the library for now; I am rolling it out to CLI2 and the VS Code extension soon.)


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.

<!-- gh-comment-id:1009318125 --> @pxl9588 commented on GitHub (Jan 10, 2022): Sorry I’m advance,I’m not very familiar with this. As far as I understand I can’t use import in a CommonJS file because it needs to be a module. So right now I don’t have a way to use the url-exist package. Did you mean there currently is a workaround or that it will be available in future releases? > On Jan 10, 2022, at 3:21 PM, David Anson ***@***.***> wrote: > >  > To clarify, while your rule needs to be CommonJS, it should be able to use an import expression to pull in an ESM dependency. Because that is an asynchronous operation, it would need to take advantage of the new support for asynchronous custom rules. (Which is only available in the library for now; I am rolling it out to CLI2 and the VS Code extension soon.) > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > Triage notifications on the go with GitHub Mobile for iOS or Android. > You are receiving this because you authored the thread.
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

Docs: https://nodejs.org/api/esm.html#import-expressions

Example: github.com/DavidAnson/markdownlint@05b9e6e43c

<!-- gh-comment-id:1009324403 --> @DavidAnson commented on GitHub (Jan 10, 2022): Docs: https://nodejs.org/api/esm.html#import-expressions Example: https://github.com/DavidAnson/markdownlint/commit/05b9e6e43c8924beb932bebb8d93d657dd49a306
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

That should work today. And since you are planning to use an asynchronous function (network access), you already needed the new support for asynchronous custom rules.

<!-- gh-comment-id:1009326151 --> @DavidAnson commented on GitHub (Jan 10, 2022): That should work today. And since you are planning to use an asynchronous function (network access), you already needed the new support for asynchronous custom rules.
Author
Owner

@pxl9588 commented on GitHub (Jan 10, 2022):

Ah right so the error isn’t about it being CommonJS, it’s about it being a module.
Which brings back the original problem,
“Cannot use import statement outside a module”. Once it’s a module markdownlint can’t ‘require’ the custom rules.cjs any more.

On Jan 10, 2022, at 3:41 PM, David Anson @.***> wrote:


Docs: https://nodejs.org/api/esm.html#import-expressions

Example: 05b9e6e


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.

<!-- gh-comment-id:1009329151 --> @pxl9588 commented on GitHub (Jan 10, 2022): Ah right so the error isn’t about it being CommonJS, it’s about it being a module. Which brings back the original problem, “Cannot use import statement outside a module”. Once it’s a module markdownlint can’t ‘require’ the custom rules.cjs any more. > On Jan 10, 2022, at 3:41 PM, David Anson ***@***.***> wrote: > >  > Docs: https://nodejs.org/api/esm.html#import-expressions > > Example: 05b9e6e > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > Triage notifications on the go with GitHub Mobile for iOS or Android. > You are receiving this because you authored the thread.
Author
Owner

@DavidAnson commented on GitHub (Jan 10, 2022):

Not an import statement, an import expression. Check the docs I link to again - I think this should work.

<!-- gh-comment-id:1009330230 --> @DavidAnson commented on GitHub (Jan 10, 2022): Not an import statement, an import expression. Check the docs I link to again - I think this should work.
Author
Owner

@DavidAnson commented on GitHub (Jan 12, 2022):

I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js

<!-- gh-comment-id:1010711888 --> @DavidAnson commented on GitHub (Jan 12, 2022): I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js
Author
Owner

@pxl9588 commented on GitHub (Jan 12, 2022):

I was also having trouble with the async so this is a great example, I will give it a try. Thank you!

On Jan 12, 2022, at 2:13 AM, David Anson @.***> wrote:


I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.

<!-- gh-comment-id:1011053148 --> @pxl9588 commented on GitHub (Jan 12, 2022): I was also having trouble with the async so this is a great example, I will give it a try. Thank you! > On Jan 12, 2022, at 2:13 AM, David Anson ***@***.***> wrote: > >  > I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > Triage notifications on the go with GitHub Mobile for iOS or Android. > You are receiving this because you authored the thread.
Author
Owner

@pxl9588 commented on GitHub (Jan 12, 2022):

Is there something I have to enable to allow async custom rules? I followed your example but I don’t see any of the on errors printing.

On Jan 12, 2022, at 8:36 AM, Patrick Landers @.***> wrote:


I was also having trouble with the async so this is a great example, I will give it a try. Thank you!

On Jan 12, 2022, at 2:13 AM, David Anson @.***> wrote:


I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.

<!-- gh-comment-id:1011308734 --> @pxl9588 commented on GitHub (Jan 12, 2022): Is there something I have to enable to allow async custom rules? I followed your example but I don’t see any of the on errors printing. > On Jan 12, 2022, at 8:36 AM, Patrick Landers ***@***.***> wrote: > >  > I was also having trouble with the async so this is a great example, I will give it a try. Thank you! > >>> On Jan 12, 2022, at 2:13 AM, David Anson ***@***.***> wrote: >>> >>  >> I wrote a simple custom rule that imports an ESM module to make sure this works like I claim: https://github.com/DavidAnson/markdownlint/blob/next/test/rules/validate-json.js >> >> — >> Reply to this email directly, view it on GitHub, or unsubscribe. >> Triage notifications on the go with GitHub Mobile for iOS or Android. >> You are receiving this because you authored the thread.
Author
Owner

@DavidAnson commented on GitHub (Jan 12, 2022):

I mentioned above that asynchronous support is only released for the library so far. CLI2 should be in a few days and the VS Code extension a week or two after that.

<!-- gh-comment-id:1011312920 --> @DavidAnson commented on GitHub (Jan 12, 2022): I mentioned above that asynchronous support is only released for the library so far. CLI2 should be in a few days and the VS Code extension a week or two after that.
Author
Owner

@pxl9588 commented on GitHub (Jan 12, 2022):

Ah, right. Okay, I’ll keep an eye out for that. Thank you for all the help.

On Jan 12, 2022, at 1:05 PM, David Anson @.***> wrote:


I mentioned above that asynchronous support is only released for the library so far. CLI2 should be in a few days and the VS Code extension a week or two after that.


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you authored the thread.

<!-- gh-comment-id:1011319730 --> @pxl9588 commented on GitHub (Jan 12, 2022): Ah, right. Okay, I’ll keep an eye out for that. Thank you for all the help. > On Jan 12, 2022, at 1:05 PM, David Anson ***@***.***> wrote: > >  > I mentioned above that asynchronous support is only released for the library so far. CLI2 should be in a few days and the VS Code extension a week or two after that. > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > Triage notifications on the go with GitHub Mobile for iOS or Android. > You are receiving this because you authored the thread.
Author
Owner

@DavidAnson commented on GitHub (Apr 11, 2022):

I think we got this sorted out and the corresponding releases should all be out.

<!-- gh-comment-id:1095519269 --> @DavidAnson commented on GitHub (Apr 11, 2022): I think we got this sorted out and the corresponding releases should all be out.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/markdownlint#391
No description provided.