Expand my Community achievements bar.

How to use JavaScript require in CQ5 (Sling)

Avatar

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