Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building node modules

Building node modules

Slides from a nodeschool workshop

Siddharth Kshetrapal

February 12, 2017
Tweet

More Decks by Siddharth Kshetrapal

Other Decks in Technology

Transcript

  1. >

  2. > npm whoami npm ERR! code ENEEDAUTH npm ERR! needs

    auth this command requires you to be logged in. npm ERR! needs auth You need to authorize this machine using
  3. > npm init name: beep-sid version: (1.0.0) description: beeper module

    entry point: (index.js) test command: git repository: keywords: beep, sound license: (MIT)
  4. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" } }
  5. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "scripts": { "test": "node index.js" } }
  6. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepTwice () { beep(); setTimeout(function () { beep(); }, 500); }
  7. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepTwice () { beep(); setTimeout(function () { beep(); }, 500); } beepTwice();
  8. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepThrice () { beep(); setTimeout(function () { beep(); }, 500); }
  9. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepThrice () { beep(); setTimeout(function () { beep(); setTimeout(function () { beep() }, 500); }, 500); }
  10. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepN (n) { if (n > 0) { beep(); n = n - 1; beepN(n); } }
  11. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } }
  12. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } } beepN(3);
  13. /* add.js */ function add (a, b) { var c

    = a + b; console.log(c); }
  14. /* add.js */ function add (a, b) { var c

    = a + b; console.log(c); beep(2); }
  15. /* add.js */ var beep = require('./index.js'); function add (a,

    b) { var c = a + b; console.log(c); beep(2); }
  16. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } }
  17. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } } module.exports = beepN;
  18. /* add.js */ var beep = require('./index.js'); function add (a,

    b) { var c = a + b; console.log(c); beep(2); } add(2, 3);
  19. /* add.js */ var beep = require('./index.js'); function add (a,

    b) { var c = a + b; console.log(c); beep(2); }
  20. /* add.js */ var beep = require('beep-sid'); function add (a,

    b) { var c = a + b; console.log(c); beep(2); }
  21. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; console.log(arguments);
  22. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2];
  23. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2]; beep(count);
  24. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "scripts": { "test": "node index.js" } }
  25. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "bin": { }, "scripts": { "test": "node index.js" } }
  26. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" } }
  27. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }
  28. /* package.json */ { "name": "beep-sid", "version": "1.1.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }
  29. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2]; beep(count);
  30. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2]; if (!count) count = 1; beep(count);
  31. /* package.json */ { "name": "beep-sid", "version": "1.1.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" } }
  32. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" } }
  33. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2]; if (count) beep(count); else { // ask for count and then beep(count) }
  34. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "inquirer": "^3.0.1" } }
  35. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "inquirer": "^3.0.1" } }
  36. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "inquirer": "3.0.1" } }
  37. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    inquirer = require('inquirer'); var arguments = process.argv; var count = arguments[2]; if (count) beep(count); else { // ask for count and then beep(count) }
  38. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?' }; }
  39. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?' }; inquirer.prompt(question) }
  40. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?' }; inquirer.prompt(question) .then(function (answer) { }); }
  41. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?' }; inquirer.prompt(question) .then(function (answer) { beep(answer.count); }); }
  42. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    inquirer = require('inquirer'); var colors = require('colors'); var arguments = process.argv; var count = arguments[2]; if (count) beep(count); else { // ask for count and then beep(count) }
  43. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?'.blue }; inquirer.prompt(question) .then(function (answer) { beep(answer.count); }); }
  44. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.1.0" },
  45. /* package.json */ { "name": "beep-sid", "version": "2.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.1.0" },
  46. /* package.json */ { "name": "beep-sid", "version": "2.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.0.1" } }
  47. /* package.json */ { "name": "beep-sid", "version": "2.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.0.1" }, "files": ["cli.js", "index.js"], "bugs": { "url" : "https://github.com/siddharthkp/beep/issues" }
  48. 1. Make sure there are no extra modules or development

    tools in dependencies. 2. Only include the files you need by using ‘files’ in your package.json. 3. Use modules which do the job and take the least amount of space.
  49. /* package.json */ { "name": "beep-sid", "version": "1.1.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "inquirer": "3.0.1" } }
  50. /* package.json */ { "name": "beep-sid", "version": "2.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.0.1" }, "files": ["index.js", "cli.js"] }
  51. 1. Make sure there are no extra modules or development

    tools in dependencies. 2. Only include the files you need by using ‘files’ in your package.json. 3. Use modules which do the job and take the least amount of space.
  52. > cost-of-modules ┌───────────┬─────────────┬────────┐ │ name │ children │ size │

    ├───────────┼─────────────┼────────┤ │ inquirer │ 26 │ 12.72M │ ├───────────┼─────────────┼────────┤ │ colors │ 0 │ 0.07M │ ├───────────┼─────────────┼────────┤ │ 2 modules │ 26 children │ 12.80M │ └───────────┴─────────────┴────────┘
  53. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    inquirer = require('inquirer'); var colors = require('colors'); var arguments = process.argv; var count = arguments[2]; if (count) beep(count); else { // ask for count and then beep(count) }
  54. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    prompt = require('prompt'); var colors = require('colors'); var arguments = process.argv; var count = arguments[2]; if (count) beep(count); else { // ask for count and then beep(count) }
  55. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?'.blue }; inquirer.prompt(question) .then(function (answer) { beep(answer.count); }); }
  56. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?'.blue }; prompt.get(question, function (answer) { beep(answer.count); }); }
  57. > cost-of-modules ┌───────────┬─────────────┬────────┐ │ name │ children │ size │

    ├───────────┼─────────────┼────────┤ │ prompt │ 31 │ 2.30M │ ├───────────┼─────────────┼────────┤ │ colors │ 0 │ 0.07M │ ├───────────┼─────────────┼────────┤ │ 2 modules │ 28 children │ 2.11M │ └───────────┴─────────────┴────────┘
  58. /* package.json */ { "name": "beep-sid", "version": "2.0.0", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.1.0" },
  59. /* package.json */ { "name": "beep-sid", "version": "2.0.1", "description": "beeper

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }, "dependencies": { "colors": "1.1.2", "enquirer": "3.1.0" },