iTranslated by AI
Running textlint on Deno
TL;DR
- Since Deno now supports npm packages, I tried running textlint on it.
- However, it requires a bit of a workaround.
- While there aren't massive benefits compared to Node.js, it's useful for those who dislike
node_modules.

The results of my experiment are published in the following repository:
Introduction
In the fall of 2022, news spread that Deno officially supported npm modules.
Hearing this, I wanted to play around with something, so I decided to try running textlint, a linting tool for text that runs on Node.js, on Deno.
textlint is used in various writing contexts such as technical manuals, academic papers, and novels. Since ease of use is key for a tool, I was curious to see how it would perform on Deno, which aims for simplicity.
The conclusion is: it worked. However, it requires some workarounds different from using it with Node.js, which I will explain in this article.
Environment
- Deno 1.29.1
- textlint v12.2.4
Running textlint
Using via CLI (Failure)
Many people likely use textlint via the CLI, such as $ textlint article.md. Since npm packages providing a CLI can also be run using something like $ deno run --allow-env --allow-read npm:{package name}, let's try that first.
$ echo "吾輩はは猫である。名前はまだないんです" > test.md
$ deno run --allow-env --allow-read --allow-sys \
npm:textlint --preset ja-technical-writing test.md
# Error
# Failed to load textlint's preset module: "ja-technical-writing" is not found.
# See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
It failed. textlint is designed such that rules (the criteria for linting) and presets (which group rules together) exist as separate npm packages. Naturally, it won't work if it can't load them.
After that, even if I imported the rule preset package before running it, it still resulted in a "Failed to load" error. When textlint loads rules and other components, it looks for the rule packages in the global or local node_modules. Since Deno's module storage is not node_modules, it can't find them even when searching.
According to the README, you can change the directory to search using the --rules-base-directory option. However, since Deno's cache directory structure is slightly different from node_modules (with version-specific directories under the package name), this doesn't work well either.
$ deno run --allow-env --allow-read --allow-sys \
npm:textlint --preset ja-technical-writing \
--rules-base-directory ~/.cache/deno/npm/registry.npmjs.org test.md
# Error
# Failed to load textlint's preset module: "ja-technical-writing" is not found.
# See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
As things stand, it seems difficult to use textlint's CLI from Deno without modifying textlint's internal logic.
Using as a module
I almost gave up here, but textlint can also be called and used as a module within a program. The Use as node module section of the official README explains how to do this.
Here is the code that worked. The trick is to import the rule preset at the beginning as well. You don't need to use the imported item, so you can just discard it. However, if you delete the line entirely, it will result in an error.
import { TextLintEngine } from "npm:textlint";
import _ from "npm:textlint-rule-preset-ja-technical-writing";
const engine = new TextLintEngine();
const results = await engine.executeOnFiles(Deno.args);
if (engine.isErrorResults(results)) console.log(engine.formatResults(results));
{
"rules": {
"preset-ja-technical-writing": true
}
}
$ deno run --allow-env --allow-read --allow-sys textlint.ts test.md

Output. Since text with errors was input, the rule is producing 3 errors.
As a limitation of this method, since we are not using the official CLI, you will need to implement any CLI features you want on your own. If you only want to specify options, you can also write them in .textlintrc.
Is it leveraging the benefits of Deno?
The commonly cited advantages of Deno over Node.js include the following:
- No
package.json: Specify the required modules by URL. - No
node_modules: Loaded modules are stored in the cache rather than the working directory. - Native support for TypeScript.
- Strong security with permission settings.
- The logo is cute.
When using textlint, the main task involves loading modules and executing them, so the fact that package.json and node_modules are not created could be a benefit.
Now, let's compare how many files are required (or generated) when starting to use it. Note that while textlint can be used both locally and globally, we will assume a local installation with CI usage in mind.
- Node.js
-
.textlintrc: Various settings -
package.json: Specification of packages to load, scripts, etc. -
package-lock.json: Generated lock file -
node_modules: Generated directory for packages
-
- Deno
-
.textlintrc: Various settings -
textlint.ts: Specification of packages to load, textlint execution
-
It would have been very cool if the CLI execution had succeeded and nothing but .textlintrc was needed, but it didn't turn out that way, resulting in 4 files versus 2 files. Well, since node_modules (notorious for being heavy) is gone, it's probably a bit of a win, right?
Adding a Rule
In the previous section, we ran textlint in its minimal state. In actual use, you will likely operate with many more rules loaded.
Let's try adding textlint-rule-ng-word, which is not included in preset-ja-technical-writing.
@@ -1,5 +1,6 @@
import { TextLintEngine } from "npm:textlint";
import _ from "npm:textlint-rule-preset-ja-technical-writing";
+import _ from "npm:textlint-rule-ng-word";
const engine = new TextLintEngine();
const results = await engine.executeOnFiles(Deno.args);
if (engine.isErrorResults(results)) console.log(engine.formatResults(results));
@@ -1,5 +1,6 @@
{
"rules": {
+ "ng-word": { "words": ["猫"] },
"preset-ja-technical-writing": true
}
}
By adding two lines and running deno run again, it will fetch the rule and execute it.
In the case of Node.js, you would add the package using npm install and then update .textlintrc. The effort involved doesn't change much here.

The first error is from the newly added rule.
Using Plugins
textlint can be extended in various ways through plugins. Here, as an example, let's add textlint-plugin-latex2e to enable reading LaTeX files.
@@ -1,6 +1,7 @@
import { TextLintEngine } from "npm:textlint";
import _ from "npm:textlint-rule-preset-ja-technical-writing";
import _ from "npm:textlint-rule-ng-word";
+import _ from "npm:textlint-plugin-latex2e";
const engine = new TextLintEngine();
const results = await engine.executeOnFiles(Deno.args);
if (engine.isErrorResults(results)) console.log(engine.formatResults(results));
@@ -1,4 +1,5 @@
{
+ "plugins": ["latex2e"],
"rules": {
"ng-word": { "words": ["猫"] },
"preset-ja-technical-writing": true
吾輩はは\textgt{猫}$e^{i\pi}+1=0$である。名前はまだないんです
Try passing a mix of file formats, like $ deno run --allow-env --allow-read --allow-sys textlint.ts test.tex test.md.

It runs without any issues!
Running CI with GitHub Actions
Since this is an article for a CI/CD calendar, I'll wrap up by running CI with GitHub Actions. In the workflow, we just set up Deno and run textlint.
Since hardcoding filenames isn't ideal, I used the find command to locate .md and .tex files and pass them via xargs.
name: CI
on: [push, pull_request]
jobs:
textlint:
name: Check by textlint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
- name: Run textlint
run: find . -type f -name '*.md' -or -name '*.tex' | xargs deno run --allow-env --allow-read --allow-sys textlint.ts
Also, to ensure the CI properly fails if errors are found, I'll specify an exit code.
@@ -4,4 +4,7 @@ import _ from "npm:textlint-rule-ng-word";
import _ from "npm:textlint-plugin-latex2e";
const engine = new TextLintEngine();
const results = await engine.executeOnFiles(Deno.args);
-if (engine.isErrorResults(results)) console.log(engine.formatResults(results));
+if (engine.isErrorResults(results)) {
+ console.error(engine.formatResults(results));
+ Deno.exit(1);
+}

Execution results in GitHub Actions
Discussion