Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 275
Prafull Dhadkar Avatar asked Jun 13 '20 22:06

Prafull Dhadkar


People also ask

Where do I put .env files?

You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables. The dotenv library reads this. env file and appends it to process.

What is a .env file in project?

.env files are used to store those variables and they should be written in all uppercase separated with underscores for naming convention. we access those variables through process.env after installing dotnev npm package.

What is an ENV file and why do I need It?

In case you are still wondering what all this means, well, you are probably new to the .env file. It’s actually a simple configuration text file that is used to define some variables you want to pass into your application’s environment. This file needs a something like a parser to make it work.

What is custom ENV in Node JS?

Custom env is a library built to make development easier by allowing multiple .env configuration for different environments. This is done by loading environment variables from a .env.envname file into the node’s process.env object.

How to create a custom environment variable for a staging development?

Just grab it with the following command: By default, custom-env picks the .env file for your dev stage. However, to customize for a different stage, add the name as a suffix as in .env.envname. We can define a custom environment variable for a staging development.

Why are my JavaScript files in a Dist folder?

Remark: The reason why the javascript files are in a dist folder is because my library is written in clojurescript. Therefore the source files are under src and the generated files are under dist. Show activity on this post.


1 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
like image 131
Behrooz Avatar answered Oct 17 '22 18:10

Behrooz