JavaScript modules and third-party libraries
Summarize
Summary of JavaScript modules and third-party libraries
JavaScript modules enable you to organize related code into reusable components within scoped applications on your ServiceNow instance. These modules, stored in thesysmoduletable, can be created or converted using the ServiceNow IDE or SDK. You can also author them in TypeScript and compile to JavaScript. Modules useexportstatements to expose code andimportorrequirestatements to reuse code in other parts of your application.
Show less
Third-party Node Package Manager (npm) libraries must be added as dependencies in your application’s package.json file to leverage their modules. You can import global Glide Server APIs from the @servicenow/glide package for server-side functionality.
Key Features
- Scoped Application Use: Modules are restricted to the scope of the application in which they are added and cannot be shared across scopes.
- Module Syntax: Supports named and default exports; named exports can include variables, constants, functions, or classes, while default exports support functions or classes.
- Importing: Use
importstatements in modules andrequirein server-side scripts like business rules or script includes. TypeScript files require explicit.tsextensions in imports. - Subpath Imports and Shorthand: The
importsfield in package.json allows defining shorthand names for module paths or dependencies, simplifying import statements. - Third-Party Libraries: Must be declared as dependencies in package.json to be incorporated into your application.
- Server API Importing: Glide APIs and other server APIs can be imported from
@servicenow/glideand its namespaces. Trusted modules must be declared innow.config.jsonfor third-party modules requiring server API access. - Script Includes: Can be imported into modules from the global scope or specific application scopes via the
@servicenow/glidepackage.
Limitations
- Global applications and customizations are unsupported on Yokohama release instances.
- Node.js APIs and global web API variables are not supported within modules.
- CommonJS third-party modules are supported only if they define exports; subpath imports do not work with CommonJS modules.
- Only ECMAScript module syntax (
importandexport) is supported within modules; scripts requirerequirefor imports. - Modules can only be modified via ServiceNow IDE or Visual Studio Code with the ServiceNow SDK.
- Third-party libraries relying on unsupported ECMAScript features or APIs cannot be used.
Practical Guidance for ServiceNow Customers
- Use JavaScript modules to organize and reuse code efficiently inside scoped applications.
- Declare third-party npm dependencies explicitly in package.json to integrate external libraries properly.
- Import ServiceNow Glide Server APIs from the
@servicenow/glidepackage to access server-side functionality inside modules. - Use the
importsfield in package.json to simplify import paths with subpath aliases. - Be mindful of the Yokohama release limitations when planning module and library usage.
- Develop and maintain modules using supported IDEs like the ServiceNow IDE or Visual Studio Code with the SDK.
Optimize your code base using JavaScript modules to group related code or add third-party libraries and reuse their code within applications.
Overview of using JavaScript modules
A module is a JavaScript file that contains related code that's shared and reused within applications on an instance. You can add JavaScript modules and third-party libraries in scoped applications that are created or converted with the ServiceNow IDE or ServiceNow SDK. You can also use TypeScript to create modules and compile them to JavaScript before building your application. After installing an application on an instance, JavaScript modules are stored in the EcmaScript Module [sys_module] table.
In a module, you identify code for reuse with export statements. Then, use import or require statements to reuse the code elsewhere in your applications. You must add third-party Node Package Manager (npm) libraries to applications as dependencies to use their module code. For general information about the syntax used to create JavaScript modules, see the JavaScript modules page on the MDN Web Docs website.
@servicenow/glide package. For more information, see Importing server APIs.Limitations
- Global applications and application customizations aren't supported with instances on the Yokohama release.
- Modules can be used only within the application scope in which they're added. They can't be used across application scopes.
- A subset of ECMAScript features are supported in modules in accordance with the JavaScript engine feature support.
- Node.js APIs aren’t supported in modules. The ServiceNow SDK build process polyfills any Node.js built-in modules while packaging modules, otherwise modules are resolved from the node_modules directory.
- Global variables related to web APIs aren’t supported.
- CommonJS modules from third-party libraries aren't supported unless they define exports. Subpath imports aren't supported with CommonJS modules. ECMAScript modules from third-party libraries are supported.
- import and export statements are only supported in modules. To import module code in scripts, such as business rules or script includes, use require statements.
- JavaScript modules [sys_module] can be modified only in the ServiceNow IDE or in Visual Studio Code with the ServiceNow SDK.
Exporting modules
export { myFunction, myVariable };Importing modules
To import the module code you want to reuse, use import statements in other modules or require statements in server-side scripts.
import { feature } from 'path/to/module';const { feature } = require('path/to/module');.ts file extension. For example, import { feature } from
'./module.ts'.{
"name": "math",
"version": "1.0.0",
"exports": {
"./functions/*.js": "./src/functions/*.js",
"./functions/private-functions/*": null
},
"imports": {
"#calc": "calculus",
"#derivative": "calculus/derivative"
},
"dependencies": {
"calculus": "1.0.0"
}
}#derivative
shorthand instead. Subpaths can also be used in the imports field to use shorthand for dependencies, such as
#calc.import { derivative } from '#derivative';
import * as calculus from '#calc';Adding third-party libraries
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"math": "1.0.0"
}
}Importing server APIs
To import server APIs and use them in a module, use import statements. Glide APIs can be imported from the @servicenow/glide package or their namespace in the package.
import { API } from '@servicenow/glide';
import { API } from '@servicenow/glide/<namespace>';import { gs } from '@servicenow/glide';
import { GlideRecord } from '@servicenow/glide';sn_ws_int namespace in a module because they run in that
namespace:import { RESTAPIRequest, RESTAPIResponse } from '@servicenow/glide/sn_ws_int';To access server APIs in a third-party library module, you must add the module as a trusted module with the trustedModules parameter in your application's now.config.json file. For more information, see Custom application configuration in source code.
For more information about available server APIs, see Server API reference.
Importing script includes
To import script includes and use them in a module, use import statements. Script includes can be imported from their application scope or the global scope in the @servicenow/glide package.
import { global } from '@servicenow/glide/global';
import { ScriptInclude } from '@servicenow/glide/<scope>';For more information about script includes, see Script includes.