In previous versions, the game supported two script formats:
-
.script(also sometimes called "Netscript 1" or "NS1") files are ran through an interpreter that is based on a version of Javascript from 2009 (ES5). -
.js(also sometimes called "Netscript 2" or "NS2") files are native javascript that is ran directly by the web browser. Modern features of javascript that are supported by your web browser are supported in.jsfiles, because they are run as native js.
Support for running .script files was removed in version 3.0.
Since all ES5 code is still valid Javascript, you may be wondering why the old code doesn't just work when renamed to .js. In this section, some key differences are explained.
- API access method: In
.scriptfiles, the game API is available at top-level scope within the file, as if they were at global scope. In.jsfiles, the game API is passed as an argument into a script'smainfunction, and is not available at top-level scope. - Execution differences: Running a
.scriptfile begins code execution at the top of the file. Running a.jsfile launches themainfunction (passing the game API in as the first argument, typically namedns). - Timing differences: In
.scriptfiles, code execution contains automatic delays between every statement. In.jsfiles, the code is being run natively so there are no builtin delays, so any needed delays must be added manually.
- Wrap the entire script inside of an exported main function, like so:
/** @param {NS} ns */
export async function main(ns) {
// your code here
}- Add
ns.as a prefix to all game functions or objects (such asns.hack()orns.args). - If a function returns a
Promise, you need to put theawaitkeyword before it (With the JSDoc comment you can hover over the function to see the return type). Note that only functions declared asasyncare allowed toawaitanything, and typically you should also await the calls to your ownasyncfunctions. - Because there are no forced delays, an infinite loop will cause the game to hang if a delay is not manually added. Such loops should
awaita function (usuallyns.sleep()) at least once to avoid this. - The
varkeyword is rarely used in modern js for declaring variables.letorconst(if the variable is never reassigned) keywords are preferred. This change is not required.
Original (early-hacking-template.script):
var target = "n00dles";
var moneyThresh = getServerMaxMoney(target) * 0.9;
var securityThresh = getServerMinSecurityLevel(target) + 5;
while (true) {
if (getServerSecurityLevel(target) > securityThresh) {
weaken(target);
} else if (getServerMoneyAvailable(target) < moneyThresh) {
grow(target);
} else {
hack(target);
}
}Migrated (early-hacking-template.js):
/** @param {NS} ns */
export async function main(ns) {
const target = "n00dles";
const moneyThresh = ns.getServerMaxMoney(target) * 0.9;
const securityThresh = ns.getServerMinSecurityLevel(target) + 5;
while (true) {
if (ns.getServerSecurityLevel(target) > securityThresh) {
await ns.weaken(target);
} else if (ns.getServerMoneyAvailable(target) < moneyThresh) {
await ns.grow(target);
} else {
await ns.hack(target);
}
}
}To get additional help with the migration, please join the official Discord server.