Things I have tried
- Searching Templater Docs
- Searching Obsidian Forum for “node_modules” + “templater”, and variations on that.
What I’m trying to do
I’m trying to write a somewhat complex user function that will be used with the Templater plugin. But i’m hitting a wall when trying to import npm packages… i’m wondering if anyone else has some insight on this?
As part of the Templater docs it states
Scripts should follow the CommonJS module specification, and export a single function.
Let’s have an example with our previous scriptmy_script.js
:
function my_function (msg) {
console.log("Message from my script:", msg);
}
module.exports = my_function;
Templater will then load all JavaScript (
.js
files) scripts in theScripts
folder.
You will then be able to call your scripts as user functions. The function name corresponds to the script file name.
So I initialised a node project:
npm init
And was building out my project fine and then when it came to installing an external package… Templater reads all the packages in node_modules
and errors because those modules don’t follow CommonJS (I assume)
Package I tried to install
npm install --save gray-matter
Has anyone else tried using a npm package and know how I might solve this? I’m guessing I could maybe move my node_modules
to a different directory than the one containing my user functions, but not 100% how they then get picked up etc.
Thanks for any help or advice