Node.js is a runtime for JS that works outside your browser, one that you'll normally interact with via the CLI. By default it comes with
npm
which is node's package manager. You need to use npm to install bothnvm
(node version managerand
yarn(another jode.js package manager). To install those, you should generally use homebrew (
brew install yarn nvm`). In general, I prefer using nvm to manage node (instead of brew) and run packages with yarn (instead of npm). Brief background but hopefully that helps with these commands.
node
(instantiates node runtime, drops you into a node interpreter ->
on the CLI indicates success)> your-file-here.js
(tells node to run the file you put in there)> .exit
(closes node runtime)
nvm install 12.5.1
(installs a specific version of node via nvm, node version manager)nvm current
(tells you which version of node you're using)nvm use 8
(tells nvm to switch your version of node, indicatd by the version number you type in)nvm alias default 12.2.0
(makes 12.2.0 your default node version)
npm install
(looks for apackage.json
file in your working directory, installs whatever packages you list there)npm install package-here
(npm adds a package to your active project innode_modules
folder ANDpackage.json
)npm install --save package-here
(deprecated, as of NPM v5.x: https://stackoverflow.com/a/19578808/241153)npm uninstall package
(removes a package from package.json ANDnode_modules
folder)
yarn add package-here
(installs a package intonode_modules
andpackage.json
dependency list)yarn add -D package-here
(install a package as one of thedevDependencies
inpackage.json
)yarn install
(reads package.json file in your working directory and installs all the packages in there)