Reusable SnippetsPractical utility code for everyday use — custom-built and ready to share
BE Node.js (JS)
Step-by-step setup for a Node.js backend (JavaScript) with ESLint v9 flat config, eslint-plugin-n, Prettier, Jest, Husky v9, and Semantic Release.
1. Install dev dependencies
Copied!
1npm install -D \
2 eslint @eslint/js globals eslint-plugin-n \
3 eslint-config-prettier eslint-plugin-prettier \
4 prettier \
5 jest supertest \
6 husky lint-staged \
7 @commitlint/config-conventional @commitlint/cli \
8 semantic-release @semantic-release/changelog @semantic-release/git \
9 @semantic-release/github @semantic-release/commit-analyzer \
10 @semantic-release/release-notes-generatorNote: eslint-plugin-n is the actively maintained fork of the deprecated eslint-plugin-node and supports ESLint v9 flat config.
2. Create eslint.config.mjs
Copied!
1import js from '@eslint/js';
2import globals from 'globals';
3import pluginN from 'eslint-plugin-n';
4import prettierConfig from 'eslint-config-prettier';
5import prettier from 'eslint-plugin-prettier';
6
7export default [
8 js.configs.recommended,
9 pluginN.configs['flat/recommended-script'],
10 {
11 languageOptions: {
12 ecmaVersion: 'latest',
13 sourceType: 'commonjs',
14 globals: { ...globals.node },
15 },
16 plugins: { prettier },
17 rules: {
18 'prettier/prettier': 'error',
19 'n/no-unsupported-features/es-syntax': 'off',
20 },
21 },
22 prettierConfig,
23];Note: Use .mjs extension to force ESM syntax regardless of the project's "type" field in package.json.
3. Create .prettierrc
Copied!
1{
2 "singleQuote": true,
3 "trailingComma": "all",
4 "printWidth": 100,
5 "tabWidth": 2,
6 "semi": true
7}4. Configure Husky and lint-staged
Add to package.json:
Copied!
1{
2 "scripts": {
3 "prepare": "husky"
4 },
5 "lint-staged": {
6 "*.js": ["eslint --fix", "prettier --write"],
7 "*.{json,md,yml}": ["prettier --write"]
8 }
9}Initialize Husky and set up hooks:
Copied!
1npx husky init
2echo "npx lint-staged" > .husky/pre-commit
3echo "npx --no -- commitlint --edit \"$1\"" > .husky/commit-msgNote: Husky v9 replaced husky install with husky init. The prepare script is now just "husky".
5. Create commitlint.config.js
Copied!
1module.exports = {
2 extends: ['@commitlint/config-conventional'],
3};6. Create jest.config.js
Copied!
1module.exports = {
2 testEnvironment: 'node',
3 testMatch: ['**/tests/**/*.test.js'],
4};7. Create release.config.js
Copied!
1module.exports = {
2 branches: ['main'],
3 plugins: [
4 '@semantic-release/commit-analyzer',
5 '@semantic-release/release-notes-generator',
6 [
7 '@semantic-release/changelog',
8 {
9 changelogFile: 'CHANGELOG.md',
10 changelogTitle: '# Changelog',
11 },
12 ],
13 [
14 '@semantic-release/git',
15 {
16 assets: ['CHANGELOG.md', 'package.json'],
17 message: 'chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}',
18 },
19 ],
20 '@semantic-release/github',
21 ],
22};In CI/CD, make sure GITHUB_TOKEN is available in the environment.
8. Add npm scripts to package.json
Copied!
1{
2 "scripts": {
3 "start": "node src/index.js",
4 "dev": "nodemon src/index.js",
5 "lint": "eslint .",
6 "format": "prettier --write .",
7 "test": "jest",
8 "test:watch": "jest --watch",
9 "release": "semantic-release"
10 }
11}9. Optional: VSCode settings
Copied!
1{
2 "editor.formatOnSave": true,
3 "editor.codeActionsOnSave": {
4 "source.fixAll.eslint": "explicit"
5 },
6 "eslint.validate": ["javascript"],
7 "prettier.enable": true
8}