Skip to content

Instantly share code, notes, and snippets.

  • Save obrunofontana/17fabac4d5d2bbd1a1c3a1a75f70207a to your computer and use it in GitHub Desktop.
Save obrunofontana/17fabac4d5d2bbd1a1c3a1a75f70207a to your computer and use it in GitHub Desktop.

Revisions

  1. @oleshkooo oleshkooo renamed this gist Mar 11, 2024. 1 changed file with 0 additions and 0 deletions.
  2. @oleshkooo oleshkooo revised this gist Aug 15, 2023. 1 changed file with 69 additions and 1 deletion.
    70 changes: 69 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -209,7 +209,7 @@ export const someData = 'some data'

    <br/>

    Run the code
    ## Run (dev)

    ```zsh
    npm run start:dev
    @@ -247,3 +247,71 @@ some data

    ✨ Done.
    ```

    <br/>

    ## Build

    ```zsh
    npm run build

    yarn build
    ```

    <br/>

    Output

    `npm`

    ```zsh
    absolute-paths $ npm run build

    > [email protected] build
    > tsc && tsc-alias
    ```

    `yarn`

    ```zsh
    absolute-paths $ yarn build
    $ tsc && tsc-alias

    ✨ Done.
    ```

    <br/>

    ## Run (build)

    ```zsh
    npm run start:prod

    yarn start:prod
    ```

    <br/>

    Output

    `npm`

    ```zsh
    absolute-paths $ npm run start:prod

    > [email protected] start:prod
    > node ./build/index.js

    some data
    ```

    `yarn`

    ```zsh
    absolute-paths $ yarn start:prod
    $ node ./build/index.js

    some data

    ✨ Done.
    ```
  3. @oleshkooo oleshkooo revised this gist Aug 15, 2023. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -93,6 +93,8 @@ yarn add -D ts-node ts-patch tsc-alias tsconfig-paths typescript-transform-paths
    ...,
    "scripts": {
    "ts-prepare": "ts-patch install -s",
    "start:dev": "npm run ts-prepare && ts-node ./src/index.ts",
    // or
    "start:dev": "yarn ts-prepare && ts-node ./src/index.ts",
    "start:prod": "node ./build/index.js",
    "build": "tsc && tsc-alias"
    @@ -124,7 +126,7 @@ Project file structure

    `package.json`

    ```json
    ```js
    {
    "name": "absolute-paths",
    "version": "1.0.0",
  4. @oleshkooo oleshkooo revised this gist Aug 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # The easiest way to set up absolute paths in your TypeScript Node.js project

    ### Step-by-step guide. Works with Nest.js or any other Node.js project. I'm going to show you how to do this quickly and easily, so let's get started.
    ### Step-by-step guide. Works with Nest.js, Express.js or any other Node.js project. I'm going to show you how to do this quickly and easily, so let's get started.

    <br/>

  5. @oleshkooo oleshkooo revised this gist Aug 15, 2023. 1 changed file with 9 additions and 5 deletions.
    14 changes: 9 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -53,11 +53,11 @@ yarn add -D ts-node ts-patch tsc-alias tsconfig-paths typescript-transform-paths

    <br/>

    4. Next, you need to customize tsconfig.json a bit:
    4. Next, you need to customize tsconfig.json a bit:

    ```js
    // tsconfig.json
    `tsconfig.json`

    ```js
    {
    "compilerOptions": {
    ...,
    @@ -86,9 +86,9 @@ yarn add -D ts-node ts-patch tsc-alias tsconfig-paths typescript-transform-paths

    5. After that, add a few scripts to the package.json file:

    ```js
    // package.json
    `package.json`

    ```js
    {
    ...,
    "scripts": {
    @@ -205,6 +205,8 @@ console.log(someData)
    export const someData = 'some data'
    ```

    <br/>

    Run the code

    ```zsh
    @@ -213,6 +215,8 @@ npm run start:dev
    yarn start:dev
    ```

    <br/>

    Output

    `npm`
  6. @oleshkooo oleshkooo revised this gist Aug 15, 2023. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -241,5 +241,3 @@ some data

    ✨ Done.
    ```

    }
  7. @oleshkooo oleshkooo created this gist Aug 15, 2023.
    245 changes: 245 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,245 @@
    # The easiest way to set up absolute paths in your TypeScript Node.js project

    ### Step-by-step guide. Works with Nest.js or any other Node.js project. I'm going to show you how to do this quickly and easily, so let's get started.

    <br/>

    ## Configure absolute paths

    1. Initialize the new package in the terminal if you haven't already done so:

    ```zsh
    npm init -y

    yarn init -y
    ```

    Your package.json should look something like this:

    ```json
    {
    "name": "absolute-paths",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT"
    }
    ```

    <br/>

    2. Similarly with tsconfig:

    ```zsh
    npx tsc --init
    ```

    And don't forget to install typescript as devDependency:

    ```zsh
    npm i -D typescript @types/node

    yarn add -D typescript @types/node
    ```

    <br/>

    3. Now let's install the necessary dependencies:

    ```zsh
    npm i -D ts-node ts-patch tsc-alias tsconfig-paths typescript-transform-paths

    yarn add -D ts-node ts-patch tsc-alias tsconfig-paths typescript-transform-paths
    ```

    <br/>

    4. Next, you need to customize tsconfig.json a bit:

    ```js
    // tsconfig.json

    {
    "compilerOptions": {
    ...,
    "baseUrl": "src" /* Specify the base directory to resolve non-relative module names. */,
    "paths": {
    "@/*": ["*"]
    } /* Specify a set of entries that re-map imports to additional lookup locations. */,

    "plugins": [
    /* Transform paths in output .js files */
    {
    "transform": "typescript-transform-paths"
    },
    /* Transform paths in output .d.ts files */
    {
    "transform": "typescript-transform-paths",
    "afterDeclarations": true
    }
    ],
    ...
    }
    }
    ```

    <br/>

    5. After that, add a few scripts to the package.json file:

    ```js
    // package.json

    {
    ...,
    "scripts": {
    "ts-prepare": "ts-patch install -s",
    "start:dev": "yarn ts-prepare && ts-node ./src/index.ts",
    "start:prod": "node ./build/index.js",
    "build": "tsc && tsc-alias"
    },
    ...
    }
    ```

    Before running the code in development mode, you need to run the `ts-prepare` command, and for a successful build, after it, run `tsc-alias`

    <br/>

    6. That's it! You can now use absolute paths in your project. You can also see some code examples below.

    <br/>

    ## Code example

    Project file structure

    ```
    ├── package.json
    ├── src
    │   ├── dir
    │   │   └── file.ts
    │   └── index.ts
    └── tsconfig.json
    ```

    `package.json`

    ```json
    {
    "name": "absolute-paths",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT",
    "scripts": {
    "ts-prepare": "ts-patch install -s",
    "start:dev": "npm run ts-prepare && ts-node ./src/index.ts",
    // or
    "start:dev": "yarn ts-prepare && ts-node ./src/index.ts",
    "start:prod": "node ./build/index.js",
    "build": "tsc && tsc-alias"
    },
    "devDependencies": {
    "@types/node": "^20.5.0",
    "ts-node": "^10.9.1",
    "ts-patch": "^3.0.2",
    "tsc-alias": "^1.8.7",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.6",
    "typescript-transform-paths": "^3.4.6"
    }
    }
    ```

    `tsconfig.json`

    ```js
    {
    "compilerOptions": {
    "target": "es2017" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
    "module": "commonjs" /* Specify what module code is generated. */,
    "outDir": "build" /* Specify an output folder for all emitted files. */,
    "strict": true /* Enable all strict type-checking options. */,
    "skipLibCheck": true /* Skip type checking all .d.ts files. */,

    /* IMPORTANT */

    "baseUrl": "src" /* Specify the base directory to resolve non-relative module names. */,
    "paths": {
    "@/*": ["*"]
    } /* Specify a set of entries that re-map imports to additional lookup locations. */,

    "plugins": [
    /* Transform paths in output .js files */
    {
    "transform": "typescript-transform-paths"
    },
    /* Transform paths in output .d.ts files */
    {
    "transform": "typescript-transform-paths",
    "afterDeclarations": true
    }
    ]

    /* End of IMPORTANT */
    },
    "ts-node": {
    "transpileOnly": true,
    "files": true
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules", "build"]
    }
    ```

    `/src/index.ts`

    ```ts
    import { someData } from '@/dir/file'

    console.log(someData)
    ```

    `/src/dir/file.ts`

    ```ts
    export const someData = 'some data'
    ```

    Run the code

    ```zsh
    npm run start:dev

    yarn start:dev
    ```

    Output

    `npm`

    ```zsh
    absolute-paths $ npm run start:dev

    > [email protected] start:dev
    > npm run ts-prepare && ts-node ./src/index.ts


    > [email protected] ts-prepare
    > ts-patch install -s

    some data
    ```

    `yarn`

    ```zsh
    absolute-paths $ yarn start:dev
    $ yarn ts-prepare && ts-node ./src/index.ts
    $ ts-patch install -s

    some data

    ✨ Done.
    ```

    }