Skip to Content
ScriptingImport JS Files

Import JavaScript Files

This feature is available in the paid version.
  • You can import functions from other JS files. Useful for code reuse and check-in to your Git repo.

Create JS File

  • Create a new JS file in the workspace.
  • Add the function you want to import in the file.
  • Export the function using module.exports.
test-import.js
// test-import.js file function helloWorld(){ return "hello world"; } module.exports = { helloWorld }

Import JS File

  • Import the JS file using require.
  • Make sure to include the .js extension in the file path.
  • The path should be relative (right-click on the file and select Copy Relative Path) to the workspace if you are using Git Sync. Otherwise, use the full path.
const { helloWorld } = require("thunder-tests/test-import.js"); var result = helloWorld(); console.log(result);

Save Path in Env Variable

  • You can also save the path in the environment variable, so you don’t have to change the path in every request if you move the file to a different folder.
var scriptPath = tc.getVar("scriptPath"); var {helloWorld} = require(scriptPath); var result = helloWorld(); console.log(result);

Autocomplete

  • To enable autocompletion, you can refer to the tc-types.d.ts file.
  • Copy the tc-types.d.ts file to your project and reference it at the top for VS Code autocomplete suggestions.
// @ts-check /// copy tc-types.d.ts file for VS Code autocompletion on the tc object /// <reference path="./tc-types.d.ts" />

TS Validation

  • Use // @ts-check at the top of the script to enable TypeScript validation.
Last updated on