JavaScript modules and third-party libraries
Summarize
Summary of JavaScript modules and third-party libraries
JavaScript modules are files containing related code that can be shared and reused within ServiceNow applications. Scoped applications created or converted with the ServiceNow SDK can utilize these modules, and TypeScript can also be employed to create and compile modules to JavaScript. All modules are stored in the EcmaScript Module [sysmodule] table. Code for reuse is identified through export statements, while import or require statements are used to access this code in different parts of the application.
Show less
Key Features
- Exporting Modules: Use export statements to identify reusable code. Named exports can include variables, constants, functions, or classes, while default exports can be functions or classes only.
- Importing Modules: Import code using import statements in modules or require statements in server-side scripts. Shorthand imports can be configured in the application's package.json file for easier access.
- Dependencies: Applications must declare dependencies on third-party libraries in their package.json file, specifying the package name and version.
- Server API Importing: Server APIs can be imported using import statements from the @servicenow/glide package or their respective namespaces.
Key Outcomes
By utilizing JavaScript modules and third-party libraries, ServiceNow customers can optimize their codebase, enhance code reuse, and streamline application development. Properly configured modules facilitate smoother integration of external libraries while adhering to ServiceNow's framework limitations. However, it is essential to note that global applications and certain unsupported functionalities are not compatible with these modules.
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 SDK. You can also use TypeScript to create modules and compile them to JavaScript before building your 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. Applications must declare dependencies on third-party Node Package Manager (npm) libraries 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.
- 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.
- Global variables related to web APIs aren’t supported.
- Third-party libraries added to your application can't access or call ServiceNow APIs.
- 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 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');{
"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 dependencies
{
"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';For more information about available server APIs, see Server API reference.