Skip to content

Instantly share code, notes, and snippets.

@uxmoon
Last active September 12, 2020 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uxmoon/91f7c010f5d3ccefa44a18f3c89b137e to your computer and use it in GitHub Desktop.
Save uxmoon/91f7c010f5d3ccefa44a18f3c89b137e to your computer and use it in GitHub Desktop.
Node - NPM command list guide for frontend web development

NPM

This is a small guide as a self-reminder :)

Requirements

Install NodeJs

Installation

Explore NPM packages using Babel as an example.

Install globally

Run the following command in your terminal to install the latest version of the package

npm i -g babel-cli

Install specific version using @ followed by the version number

npm i -g babel-cli@6.26.0

Review installation by running the following command

babel --version
// expected output: 6.24.1 (babel-core 6.26.3)

List all global packages

npm list -g --depth=0

Uninstall globally

In your terminal run the following command with the -g flag

npm uninstall -g babel-cli

Install locally

Inside the project folder run the following commands:

cd src
npm init -y
npm i babel-preset-env
  • change directory to root folder
  • initialize NPM with the -y flag to set defaults params
  • install new package for babel

Usage

cd into project's folder and run the following command babel [input file] -o [output file] --presets env where input file is the filename e.g. index.js and the desired output filename.

babel input.js -o output.js --presets env

Usage in a project

  • Input: src/index.js: es6 syntax file
  • Output: public/scripts/bundle.js, generated with babel
babel src/index.js -o public/scripts/bundle.js --presets env

Optimize and listen to changes

  • Edit package.json and add a new script with the command from above
"scripts": {
  "build": "babel src/index.js -o public/scripts/bundle.js --presets env"
},

Run the following command to transpile the file

npm run build

Watch for changes by adding a new flag at the end of the build script.

"scripts": {
  "build": "babel src/index.js -o public/scripts/bundle.js --presets env --watch"
},

Install global packages as local dependencies

First we can uninstall the babel-cli with the following command using the -g flag

npm uninstall -g babel-cli

In the project directory we run the following

npm i babel-cli@6.26.0

Our package.json will list our new package

"dependencies": {
  "babel-cli": "^6.26.0",
  "babel-preset-env": "^1.7.0",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment