Node.js
Node.js is a server-side JavaScript environment that also lets you run JavaScript in the terminal, which is pretty exciting! So far in your projects you have mainly been using JavaScript on the client-side (in the browser).
npm
npm is a package manager that is installed with Node.js. It's used to download, build, and install packages for use in our programs.
A package manager lets you use other people's JavaScript modules. We don't want to write everything from scratch or we'll be here forever! One of the wonderful things with open source programming is the ability to use and publish code to help achieve certain things. By using modules and libraries we can get to doing the fun stuff faster.
npm reads the package.json
file in the root directory of your project. It contains one JSON object which is used to provide configuration. Of particular interest are the scripts
and dependencies
properties.
Useful npm commands to understand:
npm install
- installs all the dependencies listed in yourpackage.json
file into anode_modules
foldernpm start
- used to launch the applicationnpm test
- used to launch your test suitenpm run dev
- looks for a script in yourpackage.json
called 'dev' (in this example) and runs that script
Notes:
- You can learn more about npm scripts in the npm docs
- You will see frequent references to Yarn, another package manager for Node. We use npm at Dev Academy, but the two share many similarities and you will likely encounter both in the workplace and open source projects.
- If you want to reinstall your
node_modules
, you can first delete the folder withrm -rf node_modules
and thennpm install
. - Prevent your
node_modules
folder from being committed by adding it to your.gitignore
file.- To learn more about the
.gitignore
file, read the docs - See how another project has used its
.gitignore
file
- To learn more about the
Make sure you're using an up to date version of Node and npm:
which nodenode -vnpm -v
Use NVM to keep your versions of Node and npm up to date.