Edit in GitHubLog an issue

JavaScript Modules

The beauty of plugins is that it allows you to have more than just one JavaScript file. And in order to manage elaborate projects, you might want to modularize your code and manage it from separate JS files.

To make use of modules within your code, you will need a mechanism to include the modules within other files. JavaScript has never had the #include syntax of C and its descendants, nor the import of Python. Instead, the require statement helps you do so.

Here's how to use them:

Copied to your clipboard
// specify the path of the JS file that has the required module
const { foo, bar } = require("./includeMe.js");
. . .
const result = foo(3); // will return 6

The require in UXP isn't as robust as some other include systems (e.g., it doesn't search any global paths to find the file you want), but careful use of relative paths allows you to structure your plugin directory the way you want. For example, you can have a subdirectory named lib and require a file in there this way:

const { foo, bar } = require("./lib/includeMe.js");

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.