Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

project-guidelines

> 开发工具
Open source

A set of best practices for JavaScript projects

29.5K stars0 likes3 views
WebsiteGitHub

About

A set of best practices for JavaScript projects

中文版 | 日本語版 | 한국어 | Русский | Português | Italiana | Persian/فارسی

Project Guidelines ·

While developing a new project is like rolling on a green field for you, maintaining it is a potential dark twisted nightmare for someone else. Here's a list of guidelines we've found, written and gathered that (we think) works really well with most JavaScript projects here at elsewhen. If you want to share a best practice, or think one of these guidelines should be removed, feel free to share it with us.


  • Git
    • Some Git rules
    • Git workflow
    • Writing good commit messages
  • Documentation
  • Environments
    • Consistent dev environments
    • Consistent dependencies
  • Dependencies
  • Testing
  • Structure and Naming
  • Code style
    • Some code style guidelines
    • Enforcing code style standards
  • Logging
  • API
    • API design
    • API security
    • API documentation
  • Accessibility
  • Licensing

1. Git

1.1 Some Git rules

There are a set of rules to keep in mind:

  • Perform work in a feature branch.

    Why:

    Because this way all work is done in isolation on a dedicated branch rather than the main branch. It allows you to submit multiple pull requests without confusion. You can iterate without polluting the master branch with potentially unstable, unfinished code. read more...

  • Branch out from develop

    Why:

    This way, you can make sure that code in master will almost always build without problems, and can be mostly used directly for releases (this might be overkill for some projects).

  • Never push into develop or master branch. Make a Pull Request.

    Why:

    It notifies team members that they have completed a feature. It also enables easy peer-review of the code and dedicates forum for discussing the proposed feature.

  • Update your local develop branch and do an interactive rebase before pushing your feature and making a Pull Request.

    Why:

    Rebasing will merge in the requested branch (master or develop) and apply the commits that you have made locally to the top of the history without creating a merge commit (assuming there were no conflicts). Resulting in a nice and clean history. read more ...

  • Resolve potential conflicts while rebasing and before making a Pull Request.

  • Delete local and remote feature branches after merging.

    Why:

    It will clutter up your list of branches with dead branches. It ensures you only ever merge the branch back into (master or develop) once. Feature branches should only exist while the work is still in progress.

  • Before making a Pull Request, make sure your feature branch builds successfully and passes all tests (including code style checks).

    Why:

    You are about to add your code to a stable branch. If your feature-branch tests fail, there is a high chance that your destination branch build will fail too. Additionally, you need to apply code style check before making a Pull Request. It aids readability and reduces the chance of formatting fixes being mingled in with actual changes.

  • Use this .gitignore file.

    Why:

    It already has a list of system files that should not be sent with your code into a remote repository. In addition, it excludes setting folders and files for most used editors, as well as most common dependency folders.

  • Protect your develop and master branch.

    Why:

    It protects your production-ready branches from receiving unexpected and irreversible changes. read more... GitHub, Bitbucket and GitLab

1.2 Git workflow

Because of most of the reasons above, we use Feature-branch-workflow with Interactive Rebasing and some elements of Gitflow (naming and having a develop branch). The main steps are as follows:

  • For a new project, initialize a git repository in the project directory. For subsequent features/changes this step should be ignored.

    bash
    cd <project directory>
    git init
  • Checkout a new feature/bug-fix branch.

    bash
    git checkout -b <branchname>
  • Make Changes.

    bash
    git add <file1> <file2> ...
    git commit

    Why:

    git add <file1> <file2> ... - you should add only files that make up a small and coherent change.

    git commit will start an editor which lets you separate the subject from the body.

    Read more about it in section 1.3.

    Tip:

    You could use git add -p instead, which will give you chance to review all of the introduced changes one by one, and decide whether to include them in the commit or not.

  • Sync with remote to get changes you’ve missed.

    bash
    git checkout develop
    git pull

    Why:

    This will give you a chance to deal with conflicts on your machine while rebasing (later) rather than creating a Pull Request that contains conflicts.

  • Update your feature branch with latest changes from develop by interactive rebase.

    bash
    git checkout <branchname>
    git rebase -i --autosquash develop

    Why:

    You can use --autosquash to squash all your commits to a single commit. Nobody wants many commits for a single feature in develop branch. read more...

  • If you don’t have conflicts, skip this step. If you have conflicts, resolve them and continue rebase.

    bash
    git add <file1> <file2> ...
    git rebase --continue
  • Push your branch. Rebase will change history, so you'll have to use -f to force changes into the remote branch. If someone else is working on your branch, use the less destructive --force-with-lease.

    bash
    git push -f

    Why:

    When you do a rebase, you are changing the history on your feature branch. As a result, Git will reject normal git push. Instead, you'll need to use the -f or --force flag. read more...

  • Make a Pull Request.

  • Pull request will be accepted, merged and close by a reviewer.

  • Remove your local feature branch if you're done.

    bash
    git branch -d <branchname>

    to remove all branches which are no longer on remote

    bash
    git fetch -p && for branch in `git branch -vv --no-color | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done

1.3 Writing good commit messages

Having a good guideline for creating commits and sticking to it makes working with Git and collaborating with others a lot easier. Here are some rules of thumb (source):

  • Separate the subject from the body with a newline between the two.

    Why:

    Git is smart enough to distinguish the first line of your commit message as your summary. In fact, if you try git shortlog, instead of git log, you will see a long list of commit messages, consisting of the id of the commit, and the summary only.

  • Limit the subject line to 50 characters and Wrap the body at 72 characters.

    why

    Commits should be as fine-grained and focused as possible, it is not the place to be verbose. read more...

  • Capitalize the subject line.

  • Do not end the subject line with a period.

  • Use imperative mood in the subject line.

    Why:

    Rather than writing messages that say what a committer has done. It's better to consider these messages as the instructions for what is going to be done after the commit is applied on the repository. read more...

  • Use the body to explain what and why as opposed to how.

2. Documentation

  • Use this template for README.md, Feel free to add uncovered sections.
  • For projects with more than one repository, provide links to them in their respective README.md files.
  • Keep README.md updated as a project evolves.
  • Comment your code. Try to make it as clear as possible what you are intending with each major section.
  • If there is an open discussion on GitHub or stackoverflow about the code or approach you're using, include the link in your comment.
  • Don't use comments as an excuse for a bad code. Keep your code clean.
  • Don't use clean code as an excuse to not comment at all.
  • Keep comments relevant as your code evolves.

3. Environments

  • Define separate development, test and production environments if needed.

    Why:

    Different data, tokens, APIs, ports etc... might be needed in different environments. You may want an isolated development mode that calls fake API which returns predictable data, making both automated and manual testing much easier. Or you may want to enable Google Analytics only on production and so on. read more...

  • Load your deployment specific configurations from environment variables and never add them to the codebase as constants, look at this sample.

    Why:

    You have tokens, passwords and other valuable information in there. Your config should be correctly separated from the app internals as if the codebase could be made public at any moment.

    How:

    .env files to store your variables and add them to .gitignore to be excluded. Instead, commit a .env.example which serves as a guide for developers. For production, you should still set your environment variables in the standard way. read more

  • It’s recommended to validate environment variables before your app starts. Look at this sample using joi to validate provided values. Why:

    It may save others from hours of troubleshooting.

3.1 Consistent dev environments:

  • Set your node version in engines in package.json.

    Why:

    It lets others know the version of node the project works on. read more...

  • Additionally, use nvm and create a .nvmrc in your project root. Don't forget to mention it in the documentation.

    Why:

    Any one who uses nvm can simply use nvm use to switch to the suitable node version. read more...

  • It's a good idea to setup a preinstall script that che

Issues· 16 open

View all issuesOpen on GitHub
  • #176

    AQUAEDGE PROMOTIONS

    Updated Mar 17, 2026
  • #20

    Include other editors than JetBrains in the .gitignore

    improvementUpdated Jul 22, 2021
  • #89

    Mention supported browsers in doc, also promote browserslistrc

    improvementnew sectionUpdated Jul 22, 2021
  • #108

    Lots of trailing spaces

    typo or grammatical errorUpdated Jul 22, 2021
  • #109

    Markdown syntax checker / parser before merge

    improvementUpdated Jul 22, 2021
  • #111

    dotenv-safe instead of manual checking a hardcodet whitelist

    suggestionUpdated Jul 22, 2021
  • #124

    Guidelines too opinionated about squashing commits on develop

    improvementhelp wantedUpdated Jul 22, 2021
  • #134

    Consistent API error messages

    improvementquestionUpdated Jul 22, 2021
  • #140

    Inconsistent verbiage in Coding Styles section

    improvementUpdated Jul 22, 2021
  • #160

    Refactor the guides to have inclusive language

    improvementUpdated Jul 22, 2021

> Tags

JavaScriptbest-practicesguidelineshow-tojavascript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 30, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具