Node package management, eslint and husky

wahyu eko hadi saputro
4 min readAug 28, 2023

Yarn, npm, and npx are all package managers for JavaScript projects. Here’s a brief comparison of each:

Yarn: Yarn is a package manager that was introduced as an alternative to npm. It was designed to be faster and more reliable than npm, and it uses a different package format called the “yarn package format” (YPF). Yarn also includes features like improved performance, better network performance, and a more intuitive CLI. Yarn is the default package manager for React projects.

Npm (Node package manager): Npm is the default package manager for Node.js. It stands for “Node Package Manager” and it’s the most widely used package manager for Node.js projects. Npm allows developers to easily install, share, and manage packages for their projects. Npm has a large registry of packages, and it’s the go-to choice for many developers.

Npx (Node package executor): Npx is a package manager that’s designed to be used with npm. It’s a command-line tool that allows developers to easily install and manage packages for their projects. Npx is similar to npm, but it’s designed to be more user-friendly and intuitive. Npx also includes features like automatic versioning and dependencies resolution.

Yarn.lock and package.json are created.

package.json: This file is a JSON object that contains metadata about the project, including its name, version, description, and dependencies. It’s used by npm (Node Package Manager) and yarn to determine the dependencies required by the project and install them.

yarn.lock: This file is a JSON object that contains information about the project’s dependencies and their versions. It’s used by yarn to determine the exact versions of dependencies that should be installed, based on the dependencies specified in the package.json file.

The main difference between package.json and yarn.lock is that package.json is a more high-level file that describes the project’s metadata and dependencies, while yarn.lock is a lower-level file that specifies the exact versions of dependencies that should be installed. yarn.lock is generated by yarn when you run yarn install, and it’s used to ensure that the project’s dependencies are consistent across different machines and environments.

Install eslint

Example of : .eslintrc.json

{
"rules": {
"semi": ["error", "always"],
"no-var": "error",
"no-console": "error",
"no-unused-vars": "error"
},
"parserOptions": {
"ecmaVersion": "latest"
},

"env": {
"es6": true
}
}

Install prettier

.prettierrc.json

{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}

Install husky:

Husky improves your commits and more. You can use it to lint your commit messages, run tests, lint code, etc… when you commit or push.

yarn husky install

Add yarn run eslint –fix . before commit :

Result :

Try to commit something and “yarn run eslint — fix .” is executed

github : https://github.com/wahyueko22/learn-node/tree/master/node-yarn

Ref:

--

--