HiFaraz / node-playbook

Get started fast with Node.js
http://nodeplaybook.com
1.39k stars 31 forks source link
guide javascript node-js nodejs playbook

Hi! This guide is two years old. It's still pretty good though.

I'd love some help in modernizing this please. We still need a solid playbook for the Node.js ecosystem. If you've benefited from this please consider contributing back - there's a big todo list! File an issue if you can help.

Meanwhile I have a few significant new/changed recommendations:

Thank you for the :heart: over the past two years. Hope this playbook still helps you get started fast with Node.js!

Faraz Syed


node-playbook banner

Node.js Playbook

Node.js Playbook is an opinionated "get started" guide to developing with Node.js.

The playbook helps you in two ways:

:globe_with_meridians: How to get here: nodeplaybook.com

Click here to jump to the table of contents

Who this is for

How to use this playbook

The playbook is a learning tool, and you must understand its limitations.

Do not use this playbook as your only source of learning. The playbook only gives you the most broadly applicable solution. As your needs grow you should explore other solutions. This is not the be all and end all of Node.js development. It should only be a part of your learning experience and exploration, not the whole.

How solutions were chosen

Each solution was chosen after balancing:

Contents

  1. The Golden Rule: avoid coding wherever possible
  2. General
    1. Installing Node.js
    2. Development environment
    3. Workflow
    4. File and folder structure
    5. Coding style
    6. Native modules and Windows
  3. Web
    1. Technology stack
  4. Mobile
  5. Desktop
    1. Multi-OS framework
  6. Packages
    1. Version numbering
    2. Publish to NPM
  7. Upcoming sections
  8. Contributing
    1. Acknowledgements
  9. License

The Golden Rule: avoid coding wherever possible

Be optimally lazy. There are only two principles:

  1. The fastest way to finish a task is to do nothing
  2. The second fastest way to finish a task is to get someone else to do it

(back to top)

General

This section covers general problems regardless of your development goals.

Installing Node.js

Goal

Install Node.js.

This sounds simple enough, but unfortunately the Node.js website makes you choose a version of Node.js to download.

Solution

Choose Node.js v6. It is in long term support (LTS), which means that further updates are mostly limited to bug fixes and security updates. This gives you peace of mind against major new features popping up in Node.js while you build your app.

Version 6 will stay in LTS for 18 months, and will switch to maintenance mode in April, 2018.

(back to top)

Development environment

Goal

Set up a development environment (e.g. editors, git GUIs, terminals, FTP clients) that gets you started and lets you grow according to you needs.

Solution

Download and install these tools:

(back to top)

Workflow

Goal

Set up a brand new project.

Solution

  1. Create a new repo on GitHub
    • Choose Node under the gitignore settings when creating a repo
    • Choose to create a README.md file
  2. Clone it to your computer using either a terminal command or SourceTree
  3. Set up your file and folder structure
  4. Open a terminal window in your repo folder (or use the Terminal button when you open the repo in SourceTree)
  5. Run npm init in your terminal to create a package.json file
    • Set your initial version to 0.1.0 (see also version numbering)
    • Set the entry point to src (see also file and folder structure)
    • For open source projects choose an MIT license by typing MIT when prompted for a license name
  6. Run atom . in your terminal to launch Atom in your project folder

(back to top)

File and folder structure

Goal

Set up a file and folder structure that lets you add more complexity later, such as build and test systems.

Solution

  1. Create one subfolder:
    1. src: place all your source code here
  2. Create a file in the src sub-folder called index.js

(back to top)

Coding style

Goal

Develop a coding style that lets you share your code without embarrassment.

A sloppy coding style reflects poorly on you.

Solution

Follow Airbnb's Javascript Style Guide

(back to top)

Native modules and Windows

Goal

Fix errors related to node-gyp when you try to install an npm package.

The problem is due to non-JavaScript code in the package, which npm tries to build on your computer. You will get errors unless you have the build environment installed.

Recommended solution

Find another npm package that does the job in pure JavaScript. For example, bcryptjs is a drop-in replacement for the popular bcrypt module.

How to find alternatives:

Make sure that your chosen alternative is a suitable replacement. Look for:

This is the simplest solution as it eliminates the problem rather than trying to accommodate it. The speed advantage from the native module will not be missed until later in your development cycle.

Alternative solution

Do this if and only if you cannot find a suitable alternative npm package.

Follow the Windows installation instructions at the node-gyp README.

(back to top)

Web

Read this if you are trying to build a web application.

Technology stack

Goal

Choose a technology stack (e.g. server framework, database, front-end framework, hosting platform).

Solution

(back to top)

Mobile

This section covers problems commonly encountered with:

I am seeking contributors for this section.

(back to top)

Desktop

This section covers problems commonly encountered with developing and distributing desktop applications.

I am seeking contributors for this section.

Multi-OS framework

Goal

Package and run your Node.js app as a desktop application on any operating system (OS).

Solution

Use Electron. It is now considered stable as version 1 was released on May 11, 2016. It is backed by the makers of GitHub and Atom, and has a strong community. Electron apps build and run on Mac, Windows, and Linux.

(back to top)

Packages

This section covers problems commonly encountered with developing and publishing reusable packages.

Publish to NPM

Goal

Shows how to publish your own module to npm and then install anywhere

Solution

(back to top)

Version numbering

Goal

Update version numbers in a way that indicates how significantly the package has changed for its users

Solution

Use Semantic Versioning (a.k.a. semver).

Using npm version will give you automatic package.json version updates, and will also create a git tag for you, that you can simply push with npm push --tags.

Here are the most important bits about semver:

Given a version number MAJOR.MINOR.PATCH, increment the:

  1. MAJOR version when you make incompatible API changes,
  2. MINOR version when you add functionality in a backwards-compatible manner, and
  3. PATCH version when you make backwards-compatible bug fixes.

How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

(back to top)

Upcoming sections

We'd like your help to make the Playbook more useful! Here are some sections that we'd love to receive a pull request on:

(back to top)

Contributing

Contributions are welcome! Here's how you can help:

If you want to ... How to contribute
Ask for a recommended solution to a particular problem or goal First, check the Upcoming sections. We may already be planning to work on it.

If it isn't listed in the Upcoming sections, please either create a GitHub Issue or send a pull request to add it to the Upcoming sections.
Provide a solution to a problem or goal That's great! Please create a GitHub issue to discuss the solution before creating a pull request. Please consider the criteria for accepting a solution.
Offer a better solution than one already in the Playbook. Please create a GitHub Issue to discuss this. Consider whether it meets the criteria for accepting a solution. Because we are focused on finishing upcoming sections, we are only accepting improvements if they are vastly better than the current solution.
Translate the Playbook That's great! Please join the discussion in our i18n GitHub Issue.

:heart: Acknowledgements

Thanks to the following for helping with ideas and edits

(back to top)

License

Creative Commons License Faraz Syed, 2016

Copyright (c) 2016 Faraz Syed. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

(back to top)