How to import/require Deepl API in tandem with a UXP Script for Indesign?

When I run my script in UDT debugger I this Cannot use import statement error prompt in Indesign, However once I the switch it to use to the require syntax(commonjs) it then throws the same error but on a dependency of the DeepL library which is Axios, but when I go digging into the node_modules folder and swap that import statement to use require() from within the Axios folder, I then get the same error but for a different file. At that just point I just stopped because I don’t think I should be messing with these devDeps. So how can I make this work?

I need to access both of these in the same file, :point_down:

let { app } = require("indesign");
const deepl = require('./node_modules/deepl-node');

In the deepl Docs → Your first API request - DeepL API Documentation

They say to use it like this… —> import * as deepl from ‘deepl-node’;
The Error :point_down:

I have Node -v 16.15.0. Thank you for your time

UXP isn’t Node and as such you can’t import modules/libraries as you would in a Node app. It’s a little more nuanced than that though; if a module is self contained and does not have any dependencies of its own then it’ll work, otherwise no.
You can get around this by using a bundler such as Webpack to bundle your JS and any imported modules into a single jS file for use as your UXP entry point.
Many of the UXP samples demonstrate this, but the simplest that comes to mind would be the SWC sample

1 Like

Golden! Thanks a million

1 Like

It’s worth stressing that Webpack usage is kind of inherently opinionated and so that repo is just one example of how to approach it.
It also does a couple of things specific to SWC that are not essential; I think they are documented in the readme, but including them probably won’t break your build even if they’re superfluous for your build.
Apologies if I’m teaching you to suck eggs!

Lol no biggie, I appreciate all the info