- Stable
3.0.0
- Canary
3.0.1-alpha.5
Menu
Eleventy
1.93s
Next.js
70.65s
Programmatic API
Contents
You can run Eleventy in any arbitrary Node script.
Write to the file system
Jump to section titled: Write to the file systemDon’t forget to install Eleventy into your local project first!
Now create a file called my-node-script.js
with the following contents:
my-node-script.js
import Eleventy from "@11ty/eleventy";
let elev = new Eleventy();
await elev.write();
(async function () {
const { Eleventy } = await import("@11ty/eleventy");
let elev = new Eleventy();
await elev.write();
})();
Then run your new script from the command line. Don’t include ~ $
when you run this command.
node my-node-script.js
Don’t write to the file system
Jump to section titled: Don’t write to the file systemUsing .write()
will write your output to the file system. If, instead, you want to retrieve the content programmatically without writing, use .toJSON()
or .toNDJSON()
.
JSON Output
Jump to section titled: JSON Outputmy-node-script.js
import Eleventy from "@11ty/eleventy";
let elev = new Eleventy();
let json = await elev.toJSON();
// All results
console.log(json);
(async function () {
const { Eleventy } = await import("@11ty/eleventy");
let elev = new Eleventy();
let json = await elev.toJSON();
// All results
console.log(json);
})();
ndjson Output
Jump to section titled: ndjson Outputmy-node-script.js
import Eleventy from "@11ty/eleventy";
let elev = new Eleventy();
let stream = await elev.toNDJSON();
stream.on("data", (entry) => {
// Stream one output result at a time
let json = JSON.parse(entry.toString());
console.log(json);
});
(async function () {
const { Eleventy } = await import("@11ty/eleventy");
let elev = new Eleventy();
let stream = await elev.toNDJSON();
stream.on("data", (entry) => {
// Stream one output result at a time
let json = JSON.parse(entry.toString());
console.log(json);
});
})();
Changing the Input and Output Directories
Jump to section titled: Changing the Input and Output DirectoriesThe first argument is the input directory. The second argument is the output directory.
my-node-script.js
import Eleventy from "@11ty/eleventy";
let elev = new Eleventy(".", "_site");
// Use `write` or `toJSON` or `toNDJSON`
(async function () {
const { Eleventy } = await import("@11ty/eleventy");
let elev = new Eleventy(".", "_site");
// Use `write` or `toJSON` or `toNDJSON`
})();
Full Options List
Jump to section titled: Full Options ListThe third argument to Eleventy is an options object.
(This documentation section is a work in progress but you’re welcome to dig into the Eleventy
class source code in v3.0.0
to learn more)
my-node-script.js
import Eleventy from "@11ty/eleventy";
let elev = new Eleventy(".", "_site", {
// --quiet
quietMode: true,
// --config
configPath: ".eleventy.js",
config: function (eleventyConfig) {
// Do some custom Configuration API stuff
// Works great with eleventyConfig.addGlobalData
},
});
// Use `write` or `toJSON` or `toNDJSON`
(async function () {
const { Eleventy } = await import("@11ty/eleventy");
let elev = new Eleventy(".", "_site", {
// --quiet
quietMode: true,
// --config
configPath: ".eleventy.js",
config: function (eleventyConfig) {
// Do some custom Configuration API stuff
// Works great with eleventyConfig.addGlobalData
},
});
// Use `write` or `toJSON` or `toNDJSON`
})();