- Install NVM (Node Version Manager). This will allow you to easily
swap between Node versions. See the NVM GitHub page.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
- You can install the latest version of Node by running
nvm install node
.nvm install node
- For non-personal projects, using the LTS version of Node is
recommended. You can install the latest version via
nvm install --lts
.nvm install --lts
- Add bash autocompletion to
.bashrc
or.bash_profile
.[[ -r $NVM_DIR/bash_completion ]] && . $NVM_DIR/bash_completion
- Running
npm init
in an existing folder starts an interactive prompt to create the config filepackage.json
.npm init
or
yarn init
- I recommend adding a
browserslist
field to yourpackage.json
. This is used by a few different libraries so you are targetting a certain browser version across all your dependencies. See Browserslist GitHub page for more information and options.{ "browserslist": [ "> 1%", "last 2 versions" ] }
- Add a
.gitignore
file. You’ll want to ignore node_modules at the very least! Example Node .gitignore# Logs *.log npm-debug.log* yarn-debug.log* yarn-error.log* # Dependencies node_modules/
- Add an
.nvmrc
file, with the version of Node you are using (you can find out by runningnode -v
).6.10
- Set up eslint. Eslint can be used to enforce code conventions
across projects, but for personal projects it works best for
catching errors or antipatterns.
npm install eslint --save-dev
or
yarn add eslint --dev
- You can initialise an eslint configuration file (generally
.eslintrc
) by running the following:./node_modules/.bin/eslint --init
- Some useful plugins:
- eslint-plugin-compat
{ "env": { "browser": true }, "plugins": ["compat"], "rules": { "compat/compat": 2 } }
- eslint-plugin-compat
- You can initialise an eslint configuration file (generally
- Ensure you have the correct version of Node. If the project has
an
.nvmrc
, you can runnvm use
to either use or install the correct version of Node for the project. Note that engine version is not a huge deal for client-side JavaScript, but many development tools rely on or expect a specific version of Node.nvm use
- I have a script added to my
.bash_profile
that looks for a.nvmrc
on launch.if [ -e ".nvmrc" ]; then nvm use; else nvm use default; fi
- I have a script added to my
- After cloning the repository (or fetching after some changes),
you can get the required dependencies via
npm install
.npm install
or
yarn install
- Per repo instructions will vary, but generally, running
npm start
within a repo should do something useful! You can find particular scripts via apackage.json
’sscripts
entry, and run them bynpm run foo
.
- Most Node projects provide a
start
command. This is (or should be) the entry point to a Node application. - If there is no
start
entry in yourpackage.json
, npm will runnode server.js
(if there is a file calledserver.js
in the root of your directory). - To add the start script to your application, add a
start
entry to the fieldscripts
in yourpackage.json
."scripts": { "start": "node index.js" }
- To run the start script, simply type
npm start
. - You can use the above steps for the following:
npm start
npm test
npm stop
npm restart
npm stop
npm restart
npm start
- The above scripts also support running scripts before and after
the base script by using the prefixes
pre
andpost
.npm start
npm prestart
npm start
npm poststart
npm test
npm pretest
npm test
npm posttest
npm stop
npm prestop
npm stop
npm poststop
npm restart
npm prerestart
npm prestop
npm stop
npm poststop
npm restart
npm prestart
npm start
npm poststart
npm postrestart
- You can add and run any script you like. Note that the syntax
changes slightly. If you add a script called
foo
, to execute it, you run it by executingnpm run foo
. - When creating npm scripts, a useful thing to remember is that
npm run
addsnode_modules/.bin
to thePATH
.
yarn add webpack webpack-dev-server --dev
// webpack.config.js
module.exports = {
entry: "./src/app.js",
output: {
filename: "dist/bundle.js"
}
}
}
"scripts": {
"start": "webpack-dev-server --open"
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script src="dist/bundle.js"></script>
</body>
</html>
yarn add babel-loader babel-core babel-preset-env --dev
// webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
include: "/src/",
use: {
loader: "babel-loader",
options: {
presets: [ "env" ]
}
}
}
]
}
// ...
}