NPM Introduction Using readline-sync
V School students will already have NPM installed
Click here to go to the cheet sheet
NPM, or Node Package Manager, helps us utilze 3rd party packages.
These packages will help us solve many problems that developers regularly come up against.
In terminal, if we navigate to our project folder, we can run npm init -y
This will create a package.json
file for us. This file offically makes the project a node module, and will contain meta information about our project, including what packages we used.
We can run npm install [package name]
any time to install npm packages locally, to the specific project that we're working on.
readline-sync
Still in your project folder, watch your package.json
file as you run npm install readline-sync
.
It adds a dependencies
section, and puts readline-sync and the proper version in package.json
This will be helpful when you or other developers clone down your project from github.
More importantly, it gives us the node_modules
folder and adds the readline-sync package to it.
Our file structure will now look like:
project/
/node_modules/
/readline-sync/
app.js
package.json
Remember to put /node_modules
in your git repo's .gitignore
file.
The last thing we need to do before using methods from the helpful third party package is to import it into our file using require()
.
const readline = require("readline-sync")
Here is one example from the docs, check them out for more cool methods from readline.
const userName = readline.question('May I have your name? ');
console.log('Hi ' + userName + '!');
Commands
npm init -y
- Run this command in your project folder to make it a node module
npm install [package name]
- Run this command in your project folder to install a local npm package.