Tiddlywiki on Node.js Plugins & node modules

Hey TW community,

I remember some-one mentioning that one of the benefits of having a folder-style plugin (w/ the TIDDLYWIKI_PLUGINS_PATH Environment Variable set in your command-line $env) is that you can then install node sub-modules (say as a “require” in a package.josn), and $tw can (should be able to?) access those without loading those files “as a plugin”.

This is specific to TW running on node.js. I really want to avoid packaging certain required libraries and sending them to the browser if they are literally never used there.

I think its about how you address the require() call, but the TW “module sandboxing” is throwing me… Any ideas?

Best,
Joshua Fontany

Hmm, I’m overlooking something, because the core calls require('path') and require('fs') modules no problem…

There is this, unlikely your answer, but possibly related. ## Using the external JavaScript template

1 Like

Hi @joshuafontany just to confirm that TiddlyWiki’s implementation of “require()” on Node.js will fall through to the native Node.js “require()” function if the target can’t be found as a module or tiddler. The code is here:

1 Like

@jeremyruston Ok, that makes sense. Do you have any existing plugins (not necessarily in the TW repository’s ./plugins directory) that access modules installed via a package.json’s "dependencies" field? Do I need to add the require to the tiddlywiki5 repo’s package.json? That would make installing and maintaining multiple plugins with such requirements weird.

There are examples of two different approaches in the core library:

  • The XLSX-Utils plugin installs the JSZip plugin via the dpendencies field
  • The AWS plugin expects the user to run npm install aws-sdk --save manually

That is not required.

I think the 2nd option is the closest to my scenario.

This would be run from within the tiddlywiki install directory, right?

The --save option says it automatically adds the named package to the current package’s dependencies object - in this case, the tiddlywiki package.json.

Ok, I think that’s working, and I will note the manual installation steps for any required npm package dependencies.

I think what was throwing me off, is that I have manually set my bash-command-line ENV variables’ TIDDLYWIKI_PLUGINS_PATH variable to something outside the tiddlywiki repository folder, and placed my new plugins there…

Just to be sure. The TIDDLYWIKI_PLUGINS_PATH can contain several paths. eg:

TIDDLYWIKI_PLUGINS_PATH="e:\test; d:\my\plugins; c:\your\plugins" .. 

Depending on the OS you will need `; or :` as path separators.

It still feels weird having to alter the TW repo’s package.json file to add dependencies for plugins that do not reside in that folder.

I guess the other way to go with this project would be to include TW and all my plugins as dependencies in another node.js project that launches it, like this Tiddlywiki-IPFS repo:

#!/usr/bin/env node
'use strict'

/**
 * This is invoked as a shell script by NPM when the `tiddlywiki-ipfs` command is typed
 */

const path = require('path')

// boot modules
var $tw = require('tiddlywiki').TiddlyWiki()
$tw = require('./boot/bootsuffix.js').TiddlyWiki($tw)

// Load plugin
const current = path.dirname(module.filename)
const ipfsPath = path.resolve(current, './core')
$tw.boot.extraPlugins = [`+${ipfsPath}`]

// Pass the command line arguments to the boot kernel
$tw.boot.argv = Array.prototype.slice.call(process.argv, 2)

// Boot the TW5 app
$tw.boot.boot()

I will just go with the stand-along plugins for now (not a wrapper app), as I get closer to a public demo. Thanks @jeremyruston @pmario !

1 Like