Skip to content

Instantly share code, notes, and snippets.

@rogerwschmidt
Last active September 28, 2018 13:28
Show Gist options
  • Save rogerwschmidt/904714c53a5c9b6621c28317c83c60f7 to your computer and use it in GitHub Desktop.
Save rogerwschmidt/904714c53a5c9b6621c28317c83c60f7 to your computer and use it in GitHub Desktop.

Node Ecosystem

Objectives

  • Create an npm project
  • Install npm packages
  • Require npm packages
  • Export and your own functions
  • Add scripts to package.json

Create an npm project

  • In an empty folder, initialize a new NPM project

Install npm packages

  • Install lodash as a dependency
  • Install live-server as a development dependency
  • What is the difference between a depedency and a development dependency?
  • What happens in the package.json file when a dependency is installed
  • How do you install all current dependencies that are not installed?
  • What is stored in node_modules?
  • Why should you add node_modules to `.gitignore?

Require npm packages

  • In the NPM project, create a file named app.js
  • In app.js copy the following code
const array = [1,2,3,4,5,6,7,8,9]

Export and your own functions

  • In the NPM project, create a file named shuffle.js, and add the following code
function shuffle(array){
  const arrayToShuffle = [...array]
  const result = []
  
  while(0 < arrayToShuffle.length){
    const randomIndex = Math.floor(Math.random() * arrayToShuffle.length)
    result.push(arrayToShuffle[randomIndex])

    arrayToShuffle.splice(randomIndex, 1)
  }

  return result
}
  • Export the function shuffle
  • Require your version of shuffle in app.js, and replace your usage of lodash shuffle

Add scripts to package.json

  • In package.json, add a script that will run app.js using node

Exercise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment