# New Project

## Typescript Environment

Basic environment for executing Typescript scripts.

### Commands & Configs

#### **Initialize Node Project**

```bash
npm init --init-author-name "your-email" -y
```

#### **Installing TS on Node**

Node by default don't recognize Typescript only Javascript.

```bash
npm install --save-dev @types/node
npm install --save-dev typescript
```

Generate the `tsconfig.json` file:

```bash
npx tsc --init \
--outDir build \
--module commonjs \
--allowJs true \
--removeComments true
```

or for a more strict configuration:

```bash
npx tsc --init \
--outDir build \
--esModuleInterop \
--resolveJsonModule \
--module commonjs \
--allowJs true \
--noImplicitAny true \
--removeComments true
```

You can add these to configure the folders to *include* and *exclude* in transpilation:

{% code title="tsconfig.json" %}

```json
{
    "compilerOptions": {

    },
    "include": ["./src"],
    "exclude": ["./tests"]
}
```

{% endcode %}

#### **Configure Custom Running Commands**

To be executed with `npm`.

```bash
# For Windows only (To execute the removal files/folders)
npm install --save-dev rimraf
```

{% code title="package.json" %}

```json
{
    "scripts": {
        // If Windows
        "build": "rimraf ./build && tsc",
        // If Linux
        "build": "rm -rf ./build && tsc",
        "test-build": "node build/main.js",
    }
}
```

{% endcode %}

{% hint style="info" %}
The building output directory may be configured in the `tsconfig.json` file.
{% endhint %}

#### **Optional: Install `nodemon` (For Cold Reloading)**

To auto reload the project when files change.

```bash
npm install --save-dev ts-node nodemon
```

{% code title="package.json" %}

```json
{
    "scripts": {      
        "dev": "nodemon --watch src -e ts --exec ts-node src/main.ts"
    }
}
```

{% endcode %}

{% code title="nodemon.json" %}

```json
{
    "watch": ["src"],
    "ext": ".ts,.js",
    "ignore": [],
    "exec": "npx ts-node ./src/main.ts"
}
```

{% endcode %}

#### Optional: Install `dotenv` (For Env variables)

```bash
npm install --save-dev dotenv
```

<pre class="language-json" data-title="nodemon.json"><code class="lang-json">{
    "exec": "npx ts-node <a data-footnote-ref href="#user-content-fn-1">-r dotenv/config</a> ./src/main.ts"    
}
</code></pre>

#### **Optional: Install `tsconfig-path` (???)**

```bash
npm install --save-dev tsconfig-paths
```

#### **.gitignore**

```
.DS_Store
node_modules
build
coverage
.env
!.env.example
```

### **Running**

To run + openning the browser:

```bash
npm run dev -- --open
```

Just running the server:

```bash
npm run dev
```

## Express Environment

Basic environment for executing an `Express` (RestAPI) project.

### Commands & Configs

#### Initialize the Project

Follow [#commands-and-configs](#commands-and-configs "mention") from  `Typescript Env`.

#### **Install Main Dependencies**

```bash
npm install --save express
npm install --save express-validator
npm install --save helmet
npm install --save body-parser
npm install --save-dev @types/express
```

#### **Optional: Mkdirp**

To run `mkdir -p` for create folders if don't exist.

```bash
npm install --save mkdirp
```

#### **Optional: JWT Tokens&#x20;*****(For JWT Token generation)***

{% embed url="<https://github.com/auth0/node-jsonwebtoken>" %}

```bash
npm install --save jsonwebtoken
npm install --save-dev @types/jsonwebtoken
```

#### **Optional: Async&#x20;*****(Async/Parallel execution)***

```bash
npm install --save async
```

#### **Optional: Mysql&#x20;*****(Mysql connector)***

```bash
npm install --save mysql2
```

#### **Optional: Axios**

{% embed url="<https://axios-http.com/docs/api_intro>" %}

```bash
npm install --save axios
```

#### **Optional: Express-Validator**

{% embed url="<https://express-validator.github.io/docs/>" %}

For Body, Query and Param data validation/sanitization.

```bash
npm install --save express-validator
```

[^1]: Add it to avoid having to import `dotenv` inside the code on every file.
