Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


‘require’ keyword to import modules in Illustrator JavaScript ?

Community Beginner ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

225

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Dec 27, 2020 Dec 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. 

Likes

Translate

Translate
Adobe Community Professional ,
Dec 27, 2020 Dec 27, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 02, 2021 Jan 02, 2021

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2021 Jan 04, 2021

Copy link to clipboard

Copied

LATEST

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

#include csv.js

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines