- Stable
3.0.0
Toggle Menu
Eleventy
1.93s
Gatsby
29.05s
Programmatic API
Contents
You can run Eleventy in any arbitrary Node script.
Write to the file system
Don’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
Using .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
my-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
my-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
The 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
The 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`
})();