Module Resolution

How Intellij SFCC completes and resolves require paths, the */ and ~/ prefixes, B2C modules and module.superModule along the cartridge path

SFCC's require doesn't work like Node. */ searches the cartridge path of the current site, ~/ stays in the current cartridge and module.superModule reaches the next copy of the same file. Intellij SFCC resolves these paths the way the server does, so completion, Go to Declaration and types land on the file the storefront would load

require targets, module.superModule, form links and Search Everywhere results follow the cartridge path of the site picked in the status bar, so they land on the file the server would load

Require paths

PathResolves toExample
dw/...A Script API class, see API & Typesrequire('dw/catalog/ProductMgr')
./..., ../...A file relative to the current filerequire('./helpers/cartHelpers')
~/...A file in the current cartridgerequire('~/cartridge/scripts/helpers/basketHelpers')
*/...The first cartridge on the site's cartridge path that has the filerequire('*/cartridge/scripts/helpers/productHelpers')
<cartridge>/...A file in the named cartridgerequire('app_storefront_base/cartridge/controllers/Cart')
A bare nameA B2C module or a package in the global modules folderrequire('server')

require loads .js, .ds and .json files, so completion offers all three and leaves the extension out when you accept one, the same way the server looks them up

Complete paths

require paths complete from every linked cartridge. Each item shows the cartridge that owns it; the list opens with Ctrl+Space

Start typing inside require('') and press Ctrl+Space:

  • An empty or one-word path lists your linked cartridges, marked Cartridge, and the B2C modules and global modules you can require by name
  • After */, ~/, ./ or a cartridge name, completion walks the folders and files below it
  • Each item shows the cartridge or module that owns it, so you can tell app_custom from app_storefront_base before you accept

Only linked cartridges and modules are offered. Link a missing one in Cartridges & Modules

Go to the file the server loads

Cmd+Click or Cmd+B (Ctrl+Click or Ctrl+B) on a require path opens its target. A */ path usually exists in several cartridges, and what you get depends on the site scope:

ScopeTargets
A site, for example RefArchOnly the cartridges on that site's path, in path order. The first one is the file the server loads
Global scopeEvery linked cartridge that has the file. The IDE asks you to pick one

Pick the site you are working on in the status bar to make navigation match the storefront

module.superModule

module.superModule is the module that the current file overrides: the same path in a cartridge further right on the cartridge path. It is how SFRA overrides extend the original instead of copying it

app_custom/cartridge/scripts/helpers/productHelpers.js
'use strict';

var base = module.superModule;

function getBadge(product) {
    return product.custom.badgeText || null;
}

base.getBadge = getBadge;

module.exports = base;

With a site scope selected, superModule resolves to the same file in the cartridges to the right of the current one on that site's path. In Global scope it resolves to the copies in your linked cartridges, with app_storefront_base first. Either way, base. completes the functions the original exports, and Cmd+Click on superModule opens the original

Controllers use the same mechanism with server.extend(module.superModule), then server.append, server.prepend and server.replace complete the routes of the original controller, see SFCC Contracts. Overrides creates both kinds of file for you

B2C modules and global modules

A bare require('server') doesn't point into a cartridge. On the server it loads a top-level folder next to your cartridges or a package in the special modules folder. In the IDE these come from the roots you mark yourself:

RootMark it withThen
B2C Module Root Mark Directory as › B2C Module RootIts scripts complete by module name, with the module icon
Modules (Global Modules) Root Mark Directory as › Modules (Global Modules) RootIts packages complete by name, for example server from SFRA's modules/server

See Cartridges & Modules for the rules

Troubleshooting

SymptomWhat to do
A */ path opens the wrong cartridgeSelect the site in the site scope widget. In Global scope every copy is a target
require('server') is unresolvedMark SFRA's modules folder as Modules (Global Modules) Root
A cartridge is missing from completionIt is not linked. Link it in Cartridges & Modules
module.superModule resolves to nothingNo other linked cartridge has a file at the same path below cartridge/, or the current cartridge is last on the site's path
A site's path names a cartridge that isn't in the projectFiles in a cartridge that isn't linked can't be resolved. Add its source to the project and link it

On this page