My script was to analyze specificity of my CSS selectors. # ^^^^ this script is in the same folder as npm_paths Now that we have a script to tell us where Node is, we need yet another script to execute the previous script and use those environmental variables to run the appropriate Node. Notice how it sets NODE_PATH and also gives us some good hooks to know both where node and npm is.Īlso notice how we ask npm itself where it installed things, instead of assuming “it must be in ~/.node_modules” Step 2: A shim script ( node_runner) WD-rpw 03-25-2014Įxport NODE_BIN="$NODE_BINARIES_PATH/node"Įxport GLOBAL_NODE_MODULES_PREFIX=$($NPM_BIN config get prefix)Įxport NODE_PATH=$NODE_PATH:$GLOBAL_NODE_MODULES_PREFIX/lib/node_modules/ # ^^^^^^^ avoids "npm not in path" errors. # installed via homebrew, MacPorts, NVM, etc, so askĮxport NODE_BINARIES_PATH=$(PATH=$PATH:`dirname $NPM_BIN` $NPM_BIN bin -g)
# let user's shell, with config options tell us where npm is. # This could be because of the slightly non-standard way I've installed Node
INSTALL BBEDIT 12 INSTALL
# can install modules there and call them from BBEdit scripts # ensure that NODE_PATH includes our global module folder so people # It's possible that $NODE_PATH isn't defined, and/or we need to So I created npm_paths to set the paths correctly: #!/bin/sh
INSTALL BBEDIT 12 CODE
Unless you hard code the module path, and that’s not very charitable. None of this matters if you’re using a binary like coffee, but if you’re in a Node.js script yourself and using require to access a module stored in… well, whereever the Node Package Manager stores modules, you’re going to have a bad time. I have no idea what happens if you install Node via Homebrew or Macports.Īnyway, a 100% sure way to get Node to find your modules is to include them in the NODE_PATH. It’s possible Node wasn’t placing globally installed modules in one of the default places because of how I installed Node (via NVM). But of course where npm installed the modules for me isn’t on that list.
Node has a declared list of places it will search for modules. Step 1: A shared script to set the paths right ( npm_paths) Writing a Node program that calls a globally installed module is harder. Modules like Coffeescript are easy to interact with: just call their command line tools. This is easier if the Node module includes a binary. I needed to write some Javascript code to read BBEdit input from STDIN (like you do with a text filter), do a transformation, call the node module and echo that back to BBEdit. In my work I was integrating a node module that I had installed via npm install -g. There are a few people who have integrated Node with BBEDit ( Brent Simmons and Tim Whidden), but these scripts show the basics. Today I integrated a Node module in a BBEdit Text Filter I was writing.