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 ?
Node modules that do not use the Node runtime (which there are quite a few) can be imported using the
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.
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 !
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…
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 (
Interesting. I’ll keep this in mind!