Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

How to use JavaScript require in CQ5 (Sling)


Level 10

This is a thread from StackOverflow brought here from a good answer:

I have found that Sling implements CommonJS require: But I have no idea how to use it for my case, by default require("myLib"); searches for myLib in /libs/foundation/components/primary/ folder (require("/myLib"); searches in /libs/foundation/components/primary/ as well), and I would like to use library from /libs/myProject/...

I tried to use relative path, but it ends with error:

Cannot resolve relative module name outside of a module scope.

How can I setup scope, then?

According to what I have found in code, this error occurs when thisObj of called require does not map to any ModuleScope, but how can I set, or create new ModuleScope object in JS file?

Are there any docs, or examples of CommonJS Modules for Sling?

0 Replies