Should I commit node_modules directory to git?

Published at:Published at:Updated at:

TL; DR: No. Please add node_modules to your .gitignore file:

node_modules

But, why?

The node_modules directory is where your package manager (that can be npm, yarn or pnpm) will install all the project dependencies listed on your package.json. Regardless of the package manager you choose, a lockfile (package-lock.json, yarn.lock or pnpm-lock.yaml, respectelly) will be generated in the first time you install your project dependencies, describing the entire dependency tree. This way, every time you need to reinstall your project dependencies, you shall get the exact same files.

The lockfile should be commited to git, enabling the re-installation of the tree of dependencies in any other ambient, what makes unecessary to commit the node_modules directory to git (also, it cuts the size of your repository by a lot, as node_modules can consumes gigabytes of space).

Use GitHub actions to publish your package on NPM

Published at:Published at:Updated at:

Recently, I created a package with the ESLint settings I like to use in my React projects, as I was tired of always having to configure it when I start new React projects. Publishing a NPM package is just a matter of running npm publish on the directory of your package (considering, of course, that you already have an NPM account and is authenticated on your terminal). But I wanted to automatize this publishing everytime I created a new release.

In order to do that, I used the following GitHub Action:

# File: .github/workflows/npm-publish.yml

# This workflow will publish a package to NPM when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Publish Package to npmjs

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org/
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}

If you read the YAML file above (that you should put on the .github/workflows/npm-publish.yml directory of your git repository), you should have noted that the environment variable NODE_AUTH_TOKEN should be defined. Create a new automation access token on the control panel of NPM:

  1. Access your NPM account and click in “Access tokens”: Access tokens on NPM

  2. Name your new access token and select the “Automation” type for it:

Creating access token on NPM

  1. Go to your GitHub repository, click in “Settings > Secrets > Actions > New repository secret”, name it as NODE_AUTH_TOKEN and paste the access token you just got from NPM:

Create a new secret on the GitHub repository

  1. Create a new release for your package. This should trigger our GitHub Action and publish to NPM.

Creating a new release on GitHub

Use this NPM script to create your EditorConfig files

Published at:Published at:Updated at:

Have you ever needed to create an EditorConfig to your programming projects but can never remember all the options you can use? Worry no more: just type npx create-editorconfig -y in your terminal and have one generated for you. Those are the default options:

# editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

If you want to customize the settings or even add more for a different set of files (by defining a glob pattern), just ommit the -y argument in the terminal and answer the prompted questions.

Using create-editorconfig on ZSH

What is EditorConfig?

EditorConfig is a configuration file (the .editorconfig) that defines the coding styles that a given code editor (such as VS Code) should apply in the files. You can even set a different set of rules for the files of your project according to your their extension or directory, by using a glob pattern.

My code editor is not applying the EditorConfig settings

To apply your .editorconfig rules, your code editor should have a plugin or have EditorConfig rule parsing implemented natively (if you are using VS Code, you must install this extension.

EditorConfig extension on VS Code Marketplace

How can I contribute to create-editorconfig?

Please, go to the official GitHub repository and open an issue.