Node: cannot find module 'max-api'

Ian Berman's icon

Yep, getting a cannot find module error for the max api. All of my npm packages are installed locally in my node_modules subfolder and they work just fine, but I'm not sure where max-api is exactly, or why my js file can't find it anymore after copying my project to a new folder.

my code is simple:

var mouse = require('osx-mouse'); //this works
const maxApi = require('max-api'); //this doesn't

mouse.on('move', function(x, y) {
maxApi.outlet(x, y);
maxApi.post(x, y);
});

and my (new) project filepath is

Macintosh HD / Users / speculativename / Documents / Node-IRCAM

I recently copied this project to the documents folder due to some issues with google drive folders not supporting symbolic links, which is probably what caused the issue. It was working before then. Here is my error code:

internal/modules/cjs/loader.js:589
throw err;
^
Error: Cannot find module 'max-api'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:587:15)
at Function.Module._load (internal/modules/cjs/loader.js:513:25)
at Module.require (internal/modules/cjs/loader.js:643:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/speculativename/Documents/Node-IRCAM/mouseindex.js:2:16)
at Module._compile (internal/modules/cjs/loader.js:707:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
at Module.load (internal/modules/cjs/loader.js:605:32)
at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
at Function.Module._load (internal/modules/cjs/loader.js:536:3)
[Finished in 0.1s with exit code 1]
[cmd: ['node', '/Users/speculativename/Documents/Node-IRCAM/mouseindex.js']]
[dir: /Users/speculativename/Documents/Node-IRCAM]
[path:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/speculativename/.nvm/versions/node/v11.0.0/bin:/Users/speculativename/anaconda3/bin:/Users/speculativename/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

This makes me wonder, as a relative newbie to node.js: why is it that with past project folders the max api seems to have been available globally without being installed in the node_modules folder--aka why does it "just work," whereas all the npm packages need to be configured first? Would it be worthwhile to somehow put an absolute path to the max-api framework in my require() const ?

Thanks-!

Sam Tarakajian's icon

Hi Ian,

There's a bit of magic behind the scenes to make the require call to "max-api" work. When you run a Node script from Max, it actually runs inside of a special script manager (this is how it's able to do stuff like detect when your script crashes, bind to stdout/stderr, etc.). This script manager intercepts calls to "require", and will replace any such calls to "max-api" with a special payload. In other words, you can't require "max-api" unless you're running from node.script.

Ian Berman's icon

Thanks Sam! Could have sworn I clicked script start from Max before posting, but I guess not, because it's working now. Still interesting to hear how you've implemented it.

Sam Tarakajian's icon

Happy to help! It might be useful to add that sometimes it's nice to have a script that you can run from Max or from the command line In this case you _can_ wrap the call to require("max-api") in a try/catch, but it's better to define two different entry points into your script. For example, you might have a file called "main.js" that defines and sets up your application, and another one called "max-interface.js" that requires "max-api" and defines bindings between Max and the Node application proper.

You didn't ask, but I thought I'd mention anyway :)

Ian Berman's icon

Thanks again Sam! That's a great idea, haven't had time to dive into it yet but just wanted to say it certainly sounds worthwhile...