• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

‘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

988

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

Valorous Hero , 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. 

Votes

Translate

Translate
Adobe
Valorous Hero ,
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. 

Votes

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!

Votes

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

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

#include csv.js

Votes

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
Engaged ,
May 12, 2023 May 12, 2023

Copy link to clipboard

Copied

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

Votes

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
Enthusiast ,
May 12, 2023 May 12, 2023

Copy link to clipboard

Copied

LATEST

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.

Votes

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