Skip to main content
Inspiring
December 27, 2020
Answered

‘require’ keyword to import modules in Illustrator JavaScript ?

  • December 27, 2020
  • 2 replies
  • 1881 views

Hi, I’ve written a JavaScript for Adobe Illustrator to automatically add text labels to shapes. The problem is that the shapes have sometimes odd forms (like U f.e.) it’s therefore hard to find the visual center of the shapes lying inside the shape to place the text label. I found this algorithm: A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers which seems to do the job so I tried to use it in my Illustrator JavaScript. The problem is that using this algo I need to import a module called ‘tinyqueue’.

I’m not experienced in writing JavaScript. I’ve installed node.js on my Window10 PC and installed ‘tinyqueue’ module in the directory where my Illustrator script resides (npm init -y; npm install tinyqueue). This has created a package.json file locally (+ a node_modules/ directory) . Then, as a starting point, I just added the  following line at top of my script:

var Queue = require('tinyqueue');

When trying to execute my script in Illustrator I get the following Illustrator error message:

Error 24: require is not a function. Line: 3 -> var Queue = require('tinyqueue');

I wonder if there is a problem with my package.json file? Is the ‘require’ (and f.e. ‘class’) keyword supported at all for Illustrator scripting? Do I need to add a #include line to my script to read the package.json file?

I would be grateful if someone could instruct me in solving this error!

This topic has been closed for replies.
Correct answer Silly-V

Illustrator JavaScript is an older ES3 style so there's no modules and no require available. So this code would have to be "ported" to the old sort of JavaScript that can just run in Illustrator. 

2 replies

ComacDesign
Participating Frequently
January 5, 2021

The only method I have found for "importing" external code is to use an #include e.g.

#include csv.js

bagonterman
Inspiring
May 12, 2023

what about importing into the new format? How do you do that?

Inventsable
Legend
May 13, 2023

New format as in UXP, which isn't available for Illustrator yet? You'd use modern methods like require or import but it's important to note that not everything is available in UXP and it's a custom engine, so not every 3rd party library will work out of the box like ones that rely on modules with custom UXP implementations like fs.

Silly-V
Silly-VCorrect answer
Legend
December 27, 2020

Illustrator JavaScript is an older ES3 style so there's no modules and no require available. So this code would have to be "ported" to the old sort of JavaScript that can just run in Illustrator. 

Inspiring
January 2, 2021

Hi Silly-V, thank you for your reply! I succeeded to “port” the code  A new algorithm for finding a visual center of a polygon | by Mapbox | maps for developers  for usage in Illustrator (‘tinyqueue’ module isn’t really required for this code, a simple ‘new Array()’ does the job). I succeeded to use the algo to place the labels and result is better than using the ‘center of gravity’ method but is still not satisfying in case of  'nested' shapes. I’ll post another request to see if somebody can help. Thanks again!