Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Post-Build Step for Handling .env Files in TypeScript Projects #189

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ envs.BAR // error: Property BAR does not exist on type Envs
```
If this is the case it is suggested to use [json-schema-to-ts ](https://github.com/ThomasAribart/json-schema-to-ts) to have the type always synchronized with the actual schema.

### Handling `.env` Files in TypeScript Projects


To ensure that the `.env` file is copied to the `dist/` directory after TypeScript compilation, you can add a post-build step to your `package.json` scripts:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very depending on project setup, I use TypeScript and I never copy the .env to dist file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Based on your comment, it seems that handling the .env file may be necessary when using a dynamic path setup like path.join(__dirname, isProduction ? '.env' : '.env.development'). Should I go ahead and add a code snippet to demonstrate this specific case, along with a script solution that ensures the .env file is available in the dist/ directory after TypeScript compilation? Let me know if that would be helpful!"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem isn't related to TypeScript, but when you specify a path.
You should follow what you specify.

I think a note or warning to remind is a good idea, but a full code snippets is not.
People tend to copy and paste the provided code without thinking the reason behind.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree , will make the necessary changes .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say something like

When you pointed to a specify path for your environment file, please aware your environment file should exist in the correct place. Especially in TypeScript project, the __dirname and import.meta.dirname may not be the same between development and production.


```json
"scripts": {
"build": "tsc && cp .env dist/",
"start": "node dist/app.js"
}
```
This script will compile your TypeScript code and copy the `.env` file to the `dist/` directory.

Warning: This step may not be needed for all projects. When you point to a specific path for your .env file, make sure it’s included in the build output and verify that it’s in the correct location. In TypeScript projects, paths like __dirname and import.meta.dirname can differ between development and production. Adjust according to your project’s needs to avoid confusion.


## Acknowledgements

Kindly sponsored by [Mia Platform](https://www.mia-platform.eu/)