Create an npm project
Install npm packages
Require npm packages
Export and your own functions
Add scripts to package.json
In an empty folder, initialize a new NPM project
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?
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