preveen-stack / nodejs

0 stars 0 forks source link

crash a js program #2

Open preveen-stack opened 2 months ago

preveen-stack commented 2 months ago
const fs = require('fs');

// Access an undefined variable to trigger an exception
console.log(undefinedVariable);

// Write a file asynchronously (optional)
fs.writeFile('crash.txt', 'Crash report', (err) => {
  if (err) throw err;
  console.log('Crash report saved!');
});

// Force the process to exit with an uncaught exception
process.exit(1);
sh-3.2$ node crash.js
/Users/preveen/prevlabs/nodejs/crash/crash.js:4
console.log(undefinedVariable);
            ^

ReferenceError: undefinedVariable is not defined
    at Object.<anonymous> (/Users/preveen/prevlabs/nodejs/crash/crash.js:4:13)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.7.3
preveen-stack commented 2 months ago
sh-3.2$ cat info.js 
const process = require('process');

// Print Node.js version
console.log(process.version);

// Print Node.js platform
console.log(process.platform);

// Print Node.js architecture
console.log(process.arch);

// Trigger a crash to generate the crash stack trace
function triggerCrash() {
  throw new Error('Crash triggered!');
}

triggerCrash();
sh-3.2$ node info.js 
v21.7.3
darwin
x64
/Users/preveen/prevlabs/nodejs/crash/info.js:14
  throw new Error('Crash triggered!');
  ^

Error: Crash triggered!
    at triggerCrash (/Users/preveen/prevlabs/nodejs/crash/info.js:14:9)
    at Object.<anonymous> (/Users/preveen/prevlabs/nodejs/crash/info.js:17:1)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
    at node:internal/main/run_main_module:28:49

Node.js v21.7.3