Begin
To start new project create new directory and run this commends to init node application and git:1
2yarn init
git init
I will use yarn, but you could use npm, commands are mostly the same.
To configure git lets add .gitignore file:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31################################################
# Dependencies
################################################
node_modules
################################################
# Node.js / NPM
################################################
lib-cov
*.seed
*.log
*.out
*.pid
npm-debug.log
################################################
# Miscellaneous
################################################
*~
*#
.DS_STORE
.netbeans
nbproject
.idea
.node_history
dump.rdb
.nuxt
.vscode
Linter
Let’s add some style check:1
2
3yarn add eslint
.\node_modules\.bin\eslint --init
yarn install
We will use Standard style guide in JSON format.
Let’s expand it little bit:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15{
"env": {
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 8
},
"extends": "standard",
"rules": {
"arrow-parens": ["error", "as-needed"],
"linebreak-style": ["error", "unix"]
}
}
It will extend our environment, set ECMA Script version to 8th and define some rules.
Module alias
Working with relative path is kidna hard, so lets make it easier:1
yarn add module-alias
Now we can define aliases which will makes relative paths looks like absolute one.
In the package.json we could add new section _moduleAliases and define some paths:1
2
3"_moduleAliases": {
"@": "."
}
- @ - root path
Cross env variables
To define node environment variables in package.json you could send it before running node by itself, but in the windows it won’t work, so let’s fix that:1
yarn add cross-env
Now we can add some scripts:1
2"start": "nuxt build & cross-env NODE_ENV=production node server/index.js",
"dev": "node server/index.js"
Handling date and time
Let’s use moment package for date and time:1
yarn add moment