Learn how to write a module’s fields.json file using JavaScript.
fields.json
file. However, if you prefer to not use JSON, you can configure your fields with a JavaScript file instead. Writing fields with JavaScript enables you to abstract fields that you use often, dynamically generate new fields, and more easily update existing fields.
For example, when building a set of modules, you can abstract your module fields into partials which are then pulled into individual modules. By building modules that pull from one source of truth, you’ll no longer need to update those common fields in each module individually.
Below, learn how to write fields using JavaScript, including the necessary CLI commands, along with some examples to get you started.
fields.json
file. You can use any of the following extensions:
fields.js
fields.cjs
fields.mjs
(requires Node 13.2.0+ to be installed).hs cms convert-fields
, which will save the output of the fields file locally as fields.output.json
.hs upload
or hs watch
with a --convertFields
flag. On upload, HubSpot will convert the JavaScript file to a fields.json
file.fields.json
file, it does not store the original JavaScript file. To maintain a source of truth, it’s recommended to use a version control system like Git.fields.json
file.fields.json
file. Including both will prompt you to select one or the other at upload.fieldOptions
, and must return an array of objects. Each entry in the returned array must be one of the following:.toJSON()
method. When run, this method must return a value that meets criterion 1.Promise
that resolves to a JavaScript object that meets criterion 1.hs cms convert-fields
, which saves the output of your JavaScript fields file as fields.output.json
in the same directory as the JavaScript fields file.
hs cms convert-fields
accepts the following flags:
Flag | Description |
---|---|
--src | The location of the JavaScript fields file, or a directory. |
--fieldOptions=[options] | Accepts a space-separated list that will then be passed to every exported JavaScript fields function as an array before compile-time.For example, you can configure fields to have different labels depending on whether a --fieldOptions=qa flag is set.View an example of this below. |
fields.json
file by running either hs upload
or hs watch
.
hs upload
and hs watch
accept the following flags:
Flag | Description |
---|---|
--convertFields | Enables JavaScript fields functionality. |
--saveOutput | Saves the JavaScript fields file output as fields.output.json in the same directory as the JavaScript fields file. If not included, HubSpot will save the output of the JavaScript fields file to a temporary directory then delete it after upload. |
--fieldOptions=[options] | Accepts a space-separated list that will then be passed to every exported JavaScript fields function as an array before compile-time.For example, you can configure fields to have different labels depending on whether a --fieldOptions=qa flag is set.View an example of this below. |
fields.js
file writes a regular object:
fields.js
file creates a set of common fields to be used across multiple modules:
fields.js
file changes the module’s fields based on whether the
--fieldOptions=[qa]
flag was included when running hs cms convert-fields
, hs upload
, or hs watch
:
fields.js
file includes styles from a styles.json
file:
fields.js
file includes an asynchronous call by setting the exported function as async
. If you return a Promise, the CLI will wait for the Promise to resolve before writing the fields.json
file.
fields.js
file includes an external library. Libraries such as @iGoMoon/hubspot-fields-js are supported, as their fields objects expose a .toJSON()
function.