CCSTUDIO: How does the new scripting environment in CCS20 import support JavaScript source files?

Part Number: CCSTUDIO

Tool/software:

We are attempting to port a suite of 20 DSS JavaScript test scripts from CCS version 12 to CCS version 20. That suite of JavaScript files depend upon importing source from anther 20 JavaScript source files that provide lower level support using the DSS "load" command. 

I have searched the web for information on how to import external JavaScript source files in the new scripting environment. There are a number of conflicting suggestions, several of which we have tried, but none of which have worked. They include using the require or import keywords. 

The two extremely basic example JavaScript files in the CCS20 scripting folder give no clue as to how to port a suite of 40 interdependent JavaScript files from DSS. 

Can TI provide any examples of the correct tools and syntax to use to replace the DSS load functionality in the new scripting environment? 

  • Hi Thomas,

    I have assigned this thread to the CCS Scripting expert. They should be able to provide more info soon.

    Thanks,

    Ricky

  • Thanks, Ricky. 

  • I did some experimentation and have found that the ESM syntax, which uses "module.exports" or "export" to expose shared functions or variables to other source files, and "import" {list} "from" '<exposing file>'  to include the exposed content into another source file seems to work as desired with CCS version 20 scripting under node.js and json.

    This all is done as declarations in both source and receiving source files, and is defined prior to JavaScript execution.

    As a result, when using this ESM syntax, it is not possible to expose content from one subordinate source file and import it into the top level source file dynamically during program execution.

    In the application  we are porting from DSS in CCS version 12, the "load" command is used to include files. When executed, the top level module prints an options table of operations to be performed. When one of the items in the list is selected, the "load" command is called at run time to include the source file that executes that option. This is a very clean interface because the called function to begin execution in the included file can have the same function name duplicated in each of the includable subordinate source files. There is no conflict because only one of the selectable subordinate files is included.

    So my follow-up question is:

    Is there a method when using ESM syntax to include resources from a subordinate source file that will perform the include into the higher level source file dynamically at run time, as can be done with the load command in DSS? It would be very inefficient to include all subordinate files pre-execution and have to use unique top level function names in each of the includable subordinate files, but that is the only alternative I can think of if dynamic inclusion at runtime is not possible. 

  • Is there a method when using ESM syntax to include resources from a subordinate source file that will perform the include into the higher level source file dynamically at run time, as can be done with the load command in DSS? It would be very inefficient to include all subordinate files pre-execution and have to use unique top level function names in each of the includable subordinate files, but that is the only alternative I can think of if dynamic inclusion at runtime is not possible. 

    I discussed this with engineering and they suggested looking at the dynamic import functionality:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import

    They also suggested looking at the following static import functionality:

    import * as name from “module-name”; // instead of import { foo } from “module-name”

    name.foo();

  • Ki - Thanks for forwarding the recommendation from TI Engineering. It sounds like exactly what I will need. I will let you know if I run into any issues as I convert and test my scripts. 

  • I've encountered issues with syntax errors from the node.js debugger. So I fell back to the simple example JavaScript source file provided by TI named interactive.js. When running the debugger it throws a syntax error on the first source line: 

    const repl = require("repl");
    The debugger stops immediately with error message: ReferenceError: require is not defined in ES module scope, you can use import instead. it suggests renaming the file using a *.cjs file type.
    When I run the debugger on interactive.cjs the debugger shows error message: ReferenceError: initScripting is not defined.
    Since your recommendation was to use the import ... from syntax from the ES Module syntax for including source files, I tried renaming the source file to interactive.mjs. When the debugger is run on that file, it produces the error message:  ReferenceError: require is not defined in ES module scope, you can use import instead.
    So what is the correct syntax for accomplishing the first two lines in the interactive.js example program that will not cause a node.js debugger syntax error?
  • Hi Thomas,

    Can you try the attached file instead? It is an updated one that will come with the next release. I changed the extension to txt, please rename it back to a *.mjs extension (not *.js).

    import * as repl from "repl";
    const ds = initScripting();
    console.log("Interactive scripting session started.");
    console.log("Debugger scripting object is bound to 'ds'.");
    console.log("Use Ctrl+D plus Return to exit.")
    await new Promise((resolve, reject) => {
    	try {
    		const scriptRepl = repl.start({ prompt: ">> " });
    		scriptRepl.context["ds"] = ds;
    		scriptRepl.on("exit", () => { ds.shutdown(); resolve(); });
    	} catch (e) {
    		reject(e);
    	}
    });
    

  • Ki - I tried running the new interactive.mjs program in the node.js debugger on both VSCode and CCS 20 Theia. 

    From CCS 20 Theia Run / Start debugging wants to run my project based firmware debugger. I did find a post on non-project debugging. I added the recommended clause to launch.json to the .theia folder located in my top level JavaScript folder, with the "program" line tailored to my project path hierarchy. I then opened CCS Theia to the JVScript folder that holds the interactive.mjs source file and the .theia folder with the launch.json file. The debugger produces the error message:

    C:\Program Files\nodejs\node.exe --experimental-network-inspection <project_relative_path>\JVScript\interactive.mjs
    Process exited with code 1

    When attempting to debug interactive.mjs in VSCode I get the error:
    Uncaught ReferenceError ReferenceError: initScripting is not defined
    at <anonymous> (<absolute_project_path>JVScript\interactive.mjs:9:12)
    at run (<node_internals>/internal/modules/esm/module_job:274:25)
    --- await ---
    at runEntryPointWithESMLoader (<node_internals>/internal/modules/run_main:138:19)
    at executeUserEntryPoint (<node_internals>/internal/modules/run_main:175:5)
    at <anonymous> (<node_internals>/internal/main/run_main_module:36:49)

    That seems to indicate that I need to supply VSCode with information on how to link DSS resources. but I have not found how to do that covered in the documentation.

    Can you help me configure either IDE to be able to debug the interactive.mjs JavasScript source file?

  • I didn't realize you were trying to debug it. 

    Have you seen the section on debugging a script in the user's guide?

    https://software-dl.ti.com/ccs/esd/documents/users_guide_ccs/ccs_debug-scripting.html#debugging-a-script

    However, the example there is for a standard *.js file. For a *.mjs file I needed to make the following tweaks so that my interactive.mjs file looks like:

    import scripting from 'scripting';
    import * as repl from "repl";
    
    const ds = scripting.initScripting(); // for VS Code / Theia
    
    console.log("Interactive scripting session started.");
    console.log("Debugger scripting object is bound to 'ds'.");
    console.log("Use Ctrl+D plus Return to exit.")
    await new Promise((resolve, reject) => {
    	try {
    		const scriptRepl = repl.start({ prompt: ">> " });
    		scriptRepl.context["ds"] = ds;
    		scriptRepl.on("exit", () => { ds.shutdown(); resolve(); });
    	} catch (e) {
    		reject(e);
    	}
    });

    I was then able to debug it with VS Code.

  • Ki - It was the *.mjs syntax equivalent that I was looking for. So your revised source code for interactive.mjs was what I wanted. Thanks!

    My original question assumed that interactive.mjs would duplicate the functionality of dss.bat in CCS versions 12 and older. We use dss.bat to execute a JavaScript program from the command line, which includes DSS support. That program can then print to the console and receive data typed by an operator. We need to duplicate THAT "interactive" functionality in our programs ported to CCS version 20.

    The purpose of interactive.mjs is entirely different. It allows the user to enter JavaScript commands line by line, or to execute a JavaScript program. But if there is capability to print a prompt and receive text entered by an operator when that JavaScript program is executed, I don't see how to do that. It would be helpful if the demo source code in interactive.mjs came with comments describing its purpose. 

    So, now that I have discovered that interactive.js/mjs does not provide the functionality of dss.bat, my question is:

    In the scripting environment in CCS version 20, does an equivalent to dss.bat exist, and if so, where can I find documentation on it?

    Under DSS scripting in CCS version 12, after the program is executed under dss.bat, JavaScript syntax to define and get input from the console included: 

        print( "Enter a test case number: " );
        var stdin = new BufferedReader( new InputStreamReader( System["in"] ));
       OperatorEnteredString = stdin.readLine();
       
    I know that print is replaced by console.log. Do the subsequent commands work in DSS scripting under CCS version 20? If so, how are resources needed to make them work under node.js imported using *.mjs syntax?
    BTW, I tried running interactive.mjs from a command line. It produced: 

    [53388:0815/055538.792:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
    [53388:0815/055538.793:ERROR:disk_cache.cc(208)] Unable to create cache
    [main 2025-08-15T12:55:38.875Z] update#setState idle
    [main 2025-08-15T12:55:38.878Z] Error: Error mutex already exists
    at yt.U (C:\Users\Public\Programs\VSCode-win32-x64-1.93.1\resources\app\out\vs\code\electron-main\main.js:118:71351)
    [58812:0815/055538.921:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
    [58812:0815/055538.921:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
    [58812:0815/055538.921:ERROR:cache_util_win.cc(20)] Unable to move the cache: Access is denied. (0x5)
    [58812:0815/055538.930:ERROR:disk_cache.cc(208)] Unable to create cache
    [58812:0815/055538.930:ERROR:gpu_disk_cache.cc(711)] Gpu Cache Creation failed: -2
    [58812:0815/055538.931:ERROR:disk_cache.cc(208)] Unable to create cache
    [58812:0815/055538.931:ERROR:gpu_disk_cache.cc(711)] Gpu Cache Creation failed: -2
    [58812:0815/055538.931:ERROR:disk_cache.cc(208)] Unable to create cache
    [58812:0815/055538.932:ERROR:gpu_disk_cache.cc(711)] Gpu Cache Creation failed: -2
    [58812:0815/055541.732:ERROR:service_worker_storage.cc(2016)] Failed to delete the database: Database IO error
    [main 2025-08-15T12:55:44.872Z] vscode-file: Refused to load resource c:\Users\Public\Programs\VSCode-win32-x64-1.96.0\resources\app\extensions\theme-seti\icons\seti.woff from vscode-file: protocol (original URL: vscode-file://vscode-app/c:/Users/Public/Programs/VSCode-win32-x64-1.96.0/resources/app/extensions/theme-seti/icons/seti.woff)
    [main 2025-08-15T12:56:08.876Z] update#setState checking for updates
    [main 2025-08-15T12:56:09.021Z] update#setState available for download
    When run from the terminal in VSCode, it produces:
    interactive.mjs : The term 'interactive.mjs' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + interactive.mjs
    + ~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (interactive.mjs:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    When executed from the VSCode menu command Run/Start Debugging, it produces:
    C:\Program Files\nodejs\node.exe .\interactive.mjs
    Uncaught Error Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from
    <absolue_path>\interactive.mjs
    at getPackageJSONURL (<node_internals>/internal/modules/package_json_reader:268:9)
    at packageResolve (<node_internals>/internal/modules/esm/resolve:768:81)
    at moduleResolve (<node_internals>/internal/modules/esm/resolve:854:18)
    at defaultResolve (<node_internals>/internal/modules/esm/resolve:984:11)
    at defaultResolve (<node_internals>/internal/modules/esm/loader:780:12)
    at #cachedDefaultResolve (<node_internals>/internal/modules/esm/loader:704:25)
    at resolve (<node_internals>/internal/modules/esm/loader:687:38)
    at getModuleJobForImport (<node_internals>/internal/modules/esm/loader:305:38)
    at _link (<node_internals>/internal/modules/esm/module_job:137:49)
    --- await ---
    at runEntryPointWithESMLoader (<node_internals>/internal/modules/run_main:138:19)
    at executeUserEntryPoint (<node_internals>/internal/modules/run_main:175:5)
    at <anonymous> (<node_internals>/internal/main/run_main_module:36:49)
    No debugger available, can not send 'variables'
    Process exited with code 1
    So what is the environment in which JavaScript file interactive.mjs DOES execute?
  • My original question assumed that interactive.mjs would duplicate the functionality of dss.bat in CCS versions 12 and older. We use dss.bat to execute a JavaScript program from the command line, which includes DSS support

    The dss.bat equivalent for CCS Scripting is run.bat. They are both system shell scripts that is meant to configure the environment and then passes the javascript to the launcher utility/script to execute the script. Of course how it does this under the hood is different. But the purpose of both shell scripts remains the same.

    Basically the main issue you are having is the how DSS using Rhino engine while CCS Scripting is Node,js based. This is the key change to look into. print(), readLine(), etc is all Rhino specific here.  

  • I found the new scripting API documentation in the CCS install folder .../ccs/scripting/docs/interfaces, and am working my way through the changes.

    DSS had documentation on the parameters that should be passed to DSS.bat to execute a JavaScript file. I can find no description of the parameters that can be passed to run.bat in the CCS 20 scripting environment. Can you point me to documentation on that?

    This morning I created a batch file that executes run.bat using an explicit full path to the batch file in the CCS 20 scripting folder and passed your interactive.mjs as the only parameter. It started an interactive scripting session with the message 

    "Interactive scripting session started.
    Debugger scripting object is bound to 'ds'.
    Use Ctrl+D plus Return to exit.
    >>"

    That was evidently beginner's luck. I don't believe I have changed anything, but now it fails with the message:

    "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from <absolute_path_to_interactive.mjs>\interactive.mjs"

    Do you have any idea what can have gone wrong?

  • DSS had documentation on the parameters that should be passed to DSS.bat to execute a JavaScript file. I can find no description of the parameters that can be passed to run.bat in the CCS 20 scripting environment. Can you point me to documentation on that?

    It is not a DSS/CCScripting thing. It is simply passing arguments to node.js. The dss.bat allowed for arguments to be pass to the script. The run.bat would need to be updated to support extra parameters to mode. 

    @"%~dp0\..\ccs_base\cloudagent\node.exe" "%~dp0\launcher.mjs" %1 %2 %3 %4 %5 %6 %7 %8 %9

    That would accept 9 arguments like dss.bat did.

    Then in your js script you can get the arguments like:

    const args = process.argv;

    Here is an example. I simply added the below lines to the top of the example script that comes with CCS.

     // yourScript.js
    const args = process.argv;
    
    console.log("All arguments:", args);
    console.log("First custom argument:", args[2]); // file name
    console.log("Second custom argument:", args[3]); // first argument passed to the script
    console.log("Third custom argument:", args[4]); // second argument passed to the script
    
    console.log("Hello " + args[3] + " " + args[4] + "!");
    

    And then ran it like:

    "Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from <absolute_path_to_interactive.mjs>\interactive.mjs"

    Are you running this from the command line and not trying to debug in VS Code? If so, then you need to comment out line 4 in the example script I gave earlier:

    const ds = scripting.initScripting(); // for VS Code / Theia

    You only need this to debug in VS Code. You need to remove it if running from CLI.

  • The run.bat would need to be updated to support extra parameters to mode. 

    I filed a ticket for this. Tracking link: https://sir.ext.ti.com/jira/browse/EXT_EP-12857

  • Your sample code to read and print arguments passed to run.bat is very instructive and useful. I have tried, as you did, adding the argument print logic to the front of interactive.mjs. and breakpoint.mjs.

    I am currently still stuck on the problem mentioned at the end of my last reply. Whether I run from the VSCode debugger or a CLI, whether or not I activate the line 

    const ds = scripting.initScripting();
    I get the error message: 
    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from <absolute_path>\interactive.mjs.
    Another anomaly which may be a clue: Although the first two active source code lines in each of the modified example scripts are the same as in your inteactive.mjs, i.e.
    import scripting from 'scripting';
    import * as repl from "repl";
     
    interactive.mjs is displayed in VSCode editor panel with the first line dimmed, indicating an error, and in the editor panel display of breakpoint.mjs the 2nd line is dimmed indicating an error. 
    If either script is run from CLI, the error message above is preceded by:
    node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^
  • I'm not sure why you are getting this error. You are calling the script with run.bat, correct? That should be setting up the scripting environment.

  • Apologies for the delay. Other tasks took precedence. Yes, I am running the TI scripting batch file run.bat. I call it from another batch file I created for that purpose. So the command line looks like "CCS20_RunScript.bat interactive.mjs"  which is then decoded correctly as "C:\TI\ccs2010\ccs\scripting\run.bat interactive.mjs".  It seems the possible cause could be something not correctly configured in my CCS version 20 installation or in the installation of node.js. For node resources, I tried setting the Windows PATH variable so that C:\TI\ccs2010\ccs\tools\node appears before C:\Program Files\nodejs\ and the inverse order. Results are the same. 

  • Based on your paths, it looks like CCS 20.1.0. I would recommend updating your CCS version if you can. 20.2.0 is the latest while CCS 20.3.0 should be released early next month. The older CCS 20.x versions had various stability issues unfortunately.

  • I installed CCS version 20.2.0 and modified my path variable. I still get the same result when attempting to run interactive.mjs. 

    > C:\TI\ccs2020\ccs\scripting\run.bat interactive.mjs
    node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from C:\_svn\Codebase\branches_CCSv20\NGJ_MC_02_00_DEV_TS2\DSP\SEU\JVScript\interactive.mjs
    ←[90m at new NodeError (node:internal/errors:399:5)←[39m
    ←[90m at packageResolve (node:internal/modules/esm/resolve:889:9)←[39m
    ←[90m at moduleResolve (node:internal/modules/esm/resolve:938:20)←[39m
    ←[90m at defaultResolve (node:internal/modules/esm/resolve:1153:11)←[39m
    ←[90m at nextResolve (node:internal/modules/esm/loader:163:28)←[39m
    ←[90m at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)←[39m
    ←[90m at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)←[39m
    ←[90m at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)←[39m
    ←[90m at link (node:internal/modules/esm/module_job:76:36)←[39m {
    code: ←[32m'ERR_MODULE_NOT_FOUND'←[39m
    }

    Node.js v18.16.0

    I verified that node.exe installed in C:\TI\ccs2020\ccs\tools\node IS the one being accessed. 

    > cd C:\TI\ccs2020\ccs\tools\node

    C:\TI\ccs2020\ccs\tools\node> node -v
    v18.16.0

    If I check the main installation of node.js at C:\Program Files\nodejs, it shows:

    C:\Program Files\nodejs> node -v
    v22.15.0

    It appears that the TI version of node.exe has not been updated between CCS version 20.1.0 and 20.2.0. 

    Do you know what else in my environment may need to be updated to succeed at running interactive.mjs?

  • I installed CCS version 20.2.0 and modified my path variable. I still get the same result when attempting to run interactive.mjs. 

    > C:\TI\ccs2020\ccs\scripting\run.bat interactive.mjs
    node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'scripting' imported from C:\_svn\Codebase\branches_CCSv20\NGJ_MC_02_00_DEV_TS2\DSP\SEU\JVScript\interactive.mjs
    ←[90m at new NodeError (node:internal/errors:399:5)←[39m
    ←[90m at packageResolve (node:internal/modules/esm/resolve:889:9)←[39m
    ←[90m at moduleResolve (node:internal/modules/esm/resolve:938:20)←[39m
    ←[90m at defaultResolve (node:internal/modules/esm/resolve:1153:11)←[39m
    ←[90m at nextResolve (node:internal/modules/esm/loader:163:28)←[39m
    ←[90m at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)←[39m
    ←[90m at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)←[39m
    ←[90m at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)←[39m
    ←[90m at link (node:internal/modules/esm/module_job:76:36)←[39m {
    code: ←[32m'ERR_MODULE_NOT_FOUND'←[39m
    }

    Node.js v18.16.0

    I verified that node.exe installed in C:\TI\ccs2020\ccs\tools\node IS the one being accessed. 

    > cd C:\TI\ccs2020\ccs\tools\node

    C:\TI\ccs2020\ccs\tools\node> node -v
    v18.16.0

    If I check the main installation of node.js at C:\Program Files\nodejs, it shows:

    C:\Program Files\nodejs> node -v
    v22.15.0

    It appears that the TI version of node.exe has not been updated between CCS version 20.1.0 and 20.2.0. 

    Do you know what else in my environment may need to be updated to succeed at running interactive.mjs?

  • Ki - I compared your original interactive.mjs vs. my copy. I realized that yours does NOT contain the source line 

    import scripting from 'scripting';
    I removed that line from my copy. Now both your source and mine when executed from run.bat show:
    <absolute_path> C:\TI\ccs2020\ccs\scripting\run.bat interactive.mjs
    Interactive scripting session started.
    Debugger scripting object is bound to 'ds'.
    Use Ctrl+D plus Return to exit.
    >> file:///<absolute_path>/JVScript/interactive.mjs:9
    scriptRepl.context["ds"] = ds;
    ^

    ReferenceError: ds is not defined
    at file:///<absolute_path>/JVScript/interactive.mjs:9:30
    at new Promise (<anonymous>)
    at file:///<absolute_path>/JVScript/interactive.mjs:6:7
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

    Node.js v18.16.0
    You had said that the source code line "const ds = scripting.initScripting(); // for VS Code / Theia" is only needed if running from the VSCode debugger. So I had commented it out. When I re-enabled that line for command line use, execution was normal.
    > C:\TI\ccs2020\ccs\scripting\run.bat  interactive.mjs
    Interactive scripting session started.
    Debugger scripting object is bound to 'ds'.
    Use Ctrl+D plus Return to exit.
    >>
    (To exit, press Ctrl+C again or Ctrl+D or type .exit)
    >>
    With that change in my application script, that script also starts to execute without a fault. Thanks very much for your help and patience!