Gaurav Sehrawat

Tech Consultant(Javascript/Python)

Full Stack Developer

Twitter: root3d

Github: igauravsehrawat

What is debugging?

What is debugging

Debugging Node.js

Console.log be like

console.log

Node.js Debugger

From the start

node inspect yourScript.js

In the middle of script

Insert debugger;

Shortcuts

  • continue
  • next
  • pause
  • restart
  • run
  • kill
In [1]:
    from IPython.display import IFrame    
    display(IFrame("http://nodejs.org/en/docs/guides/debugging-getting-started/", width=900, height=650))

Demo

Real world

Inspecting Running Server in Devtools

node --inspect script/server

Flags

  • --inspect-brk
  • --inspect=<port>

Recent tool by GoogleChromeLabs

NDB

NDB tool

With Additional Features Exclusively For Node.js

  • Child processes are detected and attached to
  • Place breakpoints before the modules are required
  • Edit files in UI. Devtools will save changes to the disk
  • ndb blackboxes all scripts outside current working directory

No more console.log

Deal with it

All that is fine

But never run debugger in production mode

Interesting bug exposed via debug mode:

Facebook Debug Bug

Production level debugging

  • Use the npm debug packagage
  • Use a logger (Winston, log4js, bunyan)

Debug package

  • Lot of existing packages use debug

For e.g: Express, socket.io, mongoose, many more.

Debug mode

Debug database

mongoose.set('debug', true)

Mongoose Debug

Winston package

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log` 
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
// 
if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.simple()
  }));
}
logger.log('silly', "127.0.0.1 - there's no place like home");
logger.log('debug', "127.0.0.1 - there's no place like home");
logger.log('verbose', "127.0.0.1 - there's no place like home");
logger.log('info', "127.0.0.1 - there's no place like home");
logger.log('warn', "127.0.0.1 - there's no place like home");
logger.log('error', "127.0.0.1 - there's no place like home");
logger.info("127.0.0.1 - there's no place like home");
logger.warn("127.0.0.1 - there's no place like home");
logger.error("127.0.0.1 - there's no place like home");

Sentry

If you like to get an alarm in the middle of night

https://docs.sentry.io/learn/configuration/?platform=javascript

Sentry Error

Keep monitoring them using Slack Plugin

https://sentry.io/integrations/slack/

Watching you

Demo!!

I hope it works

Other strategies

Typescript?

Elm?

Flow?

Take no suprises!!

I am the one who knocks

In [2]:
from IPython.display import IFrame    
display(IFrame("https://www.typescriptlang.org/", width=900, height=650))
In [3]:
from IPython.display import IFrame    
display(IFrame("https://elm-lang.org/", width=900, height=650))
In [4]:
from IPython.display import IFrame    
display(IFrame("https://flow.org/", width=900, height=650))

TDD

Test Driven Development

Be pro with errors

You shall not pass

Questions?

Thoughts?

Suggestions?

Dev Humor

ops problem

missing brace

No Stress

Thank you

Gaurav Sehrawat

Tech Consultant(Javascript/Python)

Full Stack Developer

Twitter: root3d

Github: igauravsehrawat