How to put .env and other filed in dist folder while building project in node?

Viewed 8468

I am using Typescript in my node project and this my tsconfig -

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist/application",
        "baseUrl": "./src",
        "paths": {
            "*": [
                "node_modules/*",
                "src/shared/types/*"
            ]
        }    
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules"
    ],
    "typedocOptions": {
        "mode": "modules",
        "out": "docs"
    }
}

This is my application folder structure -

.
├── dist
│   └── application
│       ├── app.js
│       ├── app.js.map
│       ├── modules
│       └── shared
├── environments
│   └── .env.development
├── node_modules
├── nodemon.json
├── package-lock.json
├── package.json
├── security
│   ├── domain.crt
│   └── domain.key
├── src
│   ├── app.ts
│   ├── modules
│   │   ├── chat
│   │   ├── main-controller.ts
│   │   └── sso
│   └── shared
│       ├── constants
│       ├── models
│       ├── types
│       └── utils
├── tsconfig.json
└── typedoc.json

After compilation, Typescript compiler puts everything in .dist/application.

While development, my main file (app.ts) was in ./src/app.ts. In this app.ts file, I was trying to read .env.development file in ./src/app.ts. this is how I was reading this file in app.ts -

config({ path: resolve(Utils.getAppRoot(), "../environments/.env.development") });

but at runtime system is not able to resolve the path for .env file, since TS compiler is putting app.ts code in ./dist/application/app.js, and Node is not able to find the path of .env file with respect to the current root path.

To resolve this issue, I need to copy my all the configuration/static files (here, .env file) into ./dist folder at the time of compilation.

Can someone help me to set-up the ts-config so that these static files get copied to ./dist/folder at the time of compilation?

2 Answers

I don't think you can do this using the TypeScript compiler. Instead, I recommend using NPM scripts for doing this. You can define a separate build task for each destination environment. For example, you can define build-dev task to build the project for the development environment as follows.

"build-dev": "cp .env.development ./dist/.env && tsc"

You can have a script for each environment and copy the proper .env file to the dist folder. Then, you can run these tasks using the NPM run command, for example:

npm run build-dev

The previous answer is correct with your current project structure. If you'd like to include a .env file with your TSC build you could put a .env file in the root of your project and change your tsconfig.json file to look like this

{
  "compilerOptions": {
  /* Basic Options */
  "incremental": true /* Enable incremental compilation */,
  "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
  "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
  "allowJs": true /* Allow javascript files to be compiled. */,
  "checkJs": false /* Report errors in .js files. */,
  "outDir": "./dist" /* Redirect output structure to the directory. */,
  "strict": true /* Enable all strict type-checking options. */,
  "alwaysStrict": true ,
  "rootDirs": [
    "./src",
    "./public",
    "./db"
  ] ,
  "typeRoots": [
    "@types",
    "./node_modules/@types"
  ] ,
  "esModuleInterop": true ,
  "inlineSourceMap": true ,
  "forceConsistentCasingInFileNames": true 
},
  "include": [ "./.env", "./src", "./public"],
  "exclude": ["node_modules", "test", "e2e"]
}
Related