Building node modules

Building node modules

Slides from a nodeschool workshop

Ea26ebbe66c3bb6afb5f711cfe766dff?s=128

Siddharth Kshetrapal

February 12, 2017
Tweet

Transcript

  1. None
  2. None
  3. Builging modules + publishing them on

  4. @siddharthkp javascript architect @ practo

  5. 16 000 downloads

  6. >

  7. > npm whoami

  8. > npm whoami siddharthkp

  9. > 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
  10. npmjs.com/signup

  11. > npm login

  12. > npm whoami

  13. > npm whoami siddharthkp

  14. > mkdir beep > cd beep

  15. > npm init

  16. > 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)
  17. /* index.js */

  18. /* index.js */ function beep () { process.stdout.write('\u0007'); } beep();

  19. > node index.js

  20. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

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

    module", "main": "index.js", "scripts": { "test": "node index.js" } }
  22. > npm test

  23. > npm test > npm t

  24. /* index.js */ function beep () { process.stdout.write('\u0007'); }

  25. /* index.js */ function beep () { process.stdout.write('\u0007'); }

  26. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepTwice () { }
  27. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepTwice () { beep(); beep(); } beepTwice();
  28. > npm t

  29. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepTwice () { beep(); beep(); }
  30. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

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

    beepTwice () { beep(); setTimeout(function () { beep(); }, 500); } beepTwice();
  32. > npm t

  33. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

    beepThrice () { }
  34. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

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

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

    beepN (n) { }
  37. /* index.js */ function beep () { process.stdout.write('\u0007'); } function

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

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

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } }
  40. /* 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);
  41. > npm t

  42. None
  43. /* add.js */ function add () { }

  44. /* add.js */ function add (a, b) { var c

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

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

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

    beepN (n) { if (n > 0) { beep(); n = n - 1; setTimeout(function () { beepN(n); }, 500); } }
  48. /* 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;
  49. /* add.js */ var beep = require('./index.js'); function add (a,

    b) { var c = a + b; console.log(c); beep(2); } add(2, 3);
  50. > node add.js

  51. /* add.js */ var beep = require('./index.js'); function add (a,

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

    b) { var c = a + b; console.log(c); beep(2); }
  53. > node add.js

  54. > node add.js module.js:442 throw err; ^ Error: Cannot find

    module 'beep-sid'
  55. None
  56. > npm publish

  57. > npm publish + beep-sid@1.0.0

  58. > npm install beep-sid

  59. > npm install beep-sid > node add.js

  60. None
  61. /* cli.js */

  62. #!/usr/bin/env node /* cli.js */

  63. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); beep(1);

  64. > node cli.js

  65. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); beep(1);

  66. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

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

    arguments = process.argv; console.log(arguments);
  68. > node cli.js 2

  69. > node cli.js 2 [ '/usr/local/bin/node', '/Users/siddharth/Code/learn/beep/cli.js', '2' ]

  70. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

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

    arguments = process.argv; var count = arguments[2]; beep(count);
  72. > node cli.js 2

  73. /* package.json */ { "name": "beep-sid", "version": "1.0.0", "description": "beeper

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

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

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

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

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" }
  78. None
  79. > npm publish

  80. > npm publish + beep-sid@1.1.0

  81. > sudo npm install beep-sid -g

  82. > sudo npm install beep-sid -g > beep 2

  83. > sudo npm install beep-sid -g > beep 2 >

    beep
  84. #!/usr/bin/env node /* cli.js */ var beep = require('./index.js'); var

    arguments = process.argv; var count = arguments[2]; beep(count);
  85. #!/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);
  86. /* package.json */ { "name": "beep-sid", "version": "1.1.0", "description": "beeper

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

    module", "main": "index.js", "bin": { "beep": "cli.js" }, "scripts": { "test": "node index.js" } }
  89. > npm publish + beep-sid@1.1.1

  90. > sudo npm install beep-sid -g > beep

  91. > beep ? How many beeps do you want?

  92. #!/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) }
  93. None
  94. > npm install inquirer --save

  95. /* 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" } }
  96. ^ 3 0 1

  97. ^ 3 * *

  98. ~ 3 0 1

  99. ~ 3 0 *

  100. /* 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" } }
  101. /* 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" } }
  102. #!/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) }
  103. ... else { }

  104. ... else { var question = { name: 'count', type:

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

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

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

    'input', message: 'How many beeps do you want?' }; inquirer.prompt(question) .then(function (answer) { beep(answer.count); }); }
  108. > node cli.js ? How many beeps do you want?

  109. > node cli.js ? How many beeps do you want?

  110. None
  111. > npm install colors --save

  112. #!/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) }
  113. ... else { var question = { name: 'count', type:

    'input', message: 'How many beeps do you want?'.blue }; inquirer.prompt(question) .then(function (answer) { beep(answer.count); }); }
  114. > node cli.js ? How many beeps do you want?

  115. /* 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" },
  116. /* 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" },
  117. > npm publish + beep-sid@2.0.0

  118. None
  119. /* 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" } }
  120. missing fields https://npm-janitor-api.now.sh/<username> npm docs https://docs.npmjs.com/files/package.json

  121. /* 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" }
  122. Size

  123. 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.
  124. /* 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" } }
  125. /* 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"] }
  126. 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.
  127. > sudo npm install -g cost-of-modules

  128. > cost-of-modules

  129. > cost-of-modules ┌───────────┬─────────────┬────────┐ │ name │ children │ size │

    ├───────────┼─────────────┼────────┤ │ inquirer │ 26 │ 12.72M │ ├───────────┼─────────────┼────────┤ │ colors │ 0 │ 0.07M │ ├───────────┼─────────────┼────────┤ │ 2 modules │ 26 children │ 12.80M │ └───────────┴─────────────┴────────┘
  130. None
  131. > npm uninstall inquirer --save > npm install prompt --save

  132. #!/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) }
  133. #!/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) }
  134. ... else { var question = { name: 'count', type:

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

    'input', message: 'How many beeps do you want?'.blue }; prompt.get(question, function (answer) { beep(answer.count); }); }
  136. > node cli.js ? How many beeps do you want?

  137. > cost-of-modules

  138. > cost-of-modules ┌───────────┬─────────────┬────────┐ │ name │ children │ size │

    ├───────────┼─────────────┼────────┤ │ prompt │ 31 │ 2.30M │ ├───────────┼─────────────┼────────┤ │ colors │ 0 │ 0.07M │ ├───────────┼─────────────┼────────┤ │ 2 modules │ 28 children │ 2.11M │ └───────────┴─────────────┴────────┘
  139. /* 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" },
  140. /* 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" },
  141. > npm publish + beep-sid@2.0.1

  142. now you’re an contributor

  143. @siddharthkp slides: bit.ly/npm-modules code: github.com/siddharthkp/beep newsletter: siddharthkp.github.io/#newsletter (node + react)