Import node modules

Hi erverybody,

I’m trying to import node modules into my extension. I try with require(‘…/node_modules/odoo-api/index.js’ (directory node_modules contains all I need for odoo-api), but Nova there is an error on loading extension wich say that Nova can’t find others library (here ‘got’). I think it’s just an history of wrong path or someting like that.
What’s the best way to import node_modules in an extension ? Could you share some code ?

Thanks

Node modules that utilize the Node runtime are not supported in a Nova extension. Nova does not use a node environment (it’s pure JavaScript).

Node modules that do not use the Node runtime (which there are quite a few) can be imported using the require() syntax, however. You just need to make sure that it is indeed pure, standard-library only JavaScript.

To follow up on what Logan said, I’ve seen a couple of different use cases.

If you really just want to use the code from a node module in your extension, it’s usually better to use something like rollup to bundle it into your published extension code. This will help track down issues like missing dependencies because the bundler will catch more of those errors at build time.

If you really want to use a node module that’s specifically from your user’s workspace or environment, it gets trickier, since it’s hard to guarantee that it’s the version you expect it to be, it’s installed correctly, or that it’s compatible with Nova’s JavaScriptCore runtime.
Most of what I’ve seen in this case is solved by running code via a process that interacts with the external node module - you can run a separate bit of javascript using node and the Process API, you can run a binary that the node package has, or something else.

Here’s an example of using the Process api and a bin from the workspace’s node_module - https://github.com/apexskier/nova-eslint/blob/50c679cad1137648c744c623a80f4890996a8691/src/process.ts#L107

You can also see rollup being used in that project.

Thanks for your answers, I will try with rollup !

Excuse my ignorance @apexskier. Just to clarify, using rollup does not remove the Node runtime requirement right? In other words, the package being used must still be pure javascript?

I had looked at some packages, such as micromatch, for evaluating paths using glob patterns. However, I believe these require Node.

You’re right. Rollup doesn’t remove the node runtime requirement, but it does help with issues with dynamic require calls at runtime. Anything that depends on a node standard library api that’s not implemented in Nova’s runtime will still not work.

On a separate topic, micromatch looks like it’s incompatible because it uses path. You might be able to monkey patch that with nova.path to get it working…

1 Like

Thanks for the explanation Cameron! That helps a lot.

@jason It’s a little heavy-handed, but I was actually able to get micromatch working just by including rollup-plugin-node-polyfills in my rollup config. It partially polyfills the node modules micromatch depends on (path and util).

Interesting. I’ll keep this in mind!