Import and use NPM modules in Plugin REPL
Plugin REPL is a plugin for the note taking app Obsidian that allows rapid automation of Obsidian from within Obsidian itself. It is also useful for very fast iteration when creating plugins.
This is a "companion repository" if you want to use imported JavaScript library from npm.
- Check out this repository into your vault
git clone git@github.com:talwrii/plugin-repl-imports.git - Add the npm modules you want to install to
imports.txt - Open a terminal in the checked out repository.
- Run
npm install - Run
npm run run - Run
module = replRequire("modulename")in Obsidian with plugin repl. This is like therequirefunction in node JavaScript
You do not need to understand this to use this tool, but if you are interested this is an explanation of how this works.
Obsidian is based on Electron, a tool for creating desktop apps in JavaScript. Electron is based on node js which can import modules with require using the CommonJS module system. This means that some modules (including builtin modules can be accessed with require). However, where Obsidian looks for modules is fixed, cannot be added to and is readonly.
To get around this plugins are built with the JavaScript "bundler" esbuild this embeds a copy of all the code that you import into one big js file - similar to the approach that web pages use, but with the exception that some modules are still imported using require such as obsidian modules.
This repository uses the same approch.
This approach is based on how plugins are built for Obsidian, code is directly taken from the MIT-licensed sample plugin repository for Obsidian.