typescript not reading env file - node.js

I am new to typescript and i built this simple mailer application, but the problem is that TypeScript is unable to read the file, when i put it in the src folder it was not copied in dist on build, when i put it outside the src folder and put the manual require just to test IDE did not showed me option for env
and here is my directory tree
what causes that? i dont have a clue.

I don't think node (or typescript) knows how to import a .env file natively.
You probably want to install and configure the dotenv npm package
Then you just add this line to the file that boots your server:
require('dotenv').config()
And now your env vars are accessible at:
process.ENV.MY_VAR_HERE
Here's a decent article on environment variables and how a .env file works with them.

Related

build the application, and an environment variable error occurs when accessing after build

I am using Nuxt3 and Prisma to build the application, in the development environment, they run fine, when I execute the npm run buildcommand, there is also no output error.nuxt3 outputs the .output folder.
build
But when I use the command start to run, prisma prompts me environment variable could not be found.
error
I recreated the .env file under the output .prisma folder, but it doesn't work.
recreated
How can I solve the environment variable in production problem?
Your .env file should be in the same directory as your schema.prisma file. I'm not sure what framework/library you are using, but it looks like it emits a schema.prisma file within node_modules, which is not where your environment variables should go. Wherever you actually have your schema.prisma is where the .env file should go.
Take a look at the Prisma documentation about environment variables if you need help configuring where it's supposed to go: https://www.prisma.io/docs/guides/development-environment/environment-variables

Where do compiled NodeJs apps read the dotenv file from at execute time?

I am writing a commanderjs app, that uses dotenv. The app is that is compiled using pkg .
I have compiled the app for linux and moved the binary to /usr/local/bin, for it to be available on my PATH.
I am running the app from a shell script in ~/testing/myscript.sh
When I run the script from e.g. ~/testing where does the compiled app read the .env file from?
It reads the .env from the executing directory, in this case ~/testing/.env
This was originally failing because I had named the file myapp.env, the file must be called .env.
Alternatively, the config path can be set using a path in a object passed to .config():
dotenv.config({path:'myapp.env'});

HTML .gitignore

I currently have my api keys and list id sitting in my app.js file. I am trying to add those API keys to another file and hide the file using .gitignore. But how do I do that and even if I hide the file in the new secret file how do I get those keys back in my original app.js file
What you should do is use an Environment File.
Environment files start with .env and allow you to save things that are specific to your working environment (such as sensitive keys).
If you are using nodejs, you can easily import the data from a .env file using require('dotenv').config().
These files are also ignored by the default .gitignore configuration.
Firstly install a library called dotenv
npm i dotenv --save
Create a file called .env and place your API KEY in it and create .gitignore file place your .env in it
Example
Inside .env :
API_KEY= YOUR_APIKEY without quotation
Inside .gitignore:
.env
To use your API KEY inside app.js
Inside app.js:
require('dotenv').config()
let API_KEY = process.env.API_KEY;
Using .env file
Install the dotenv npm package
npm install dotenv --save
Create a .env file at the root of your project and store your SECRET_KEY like shown below.
SECRET_KEY="YOURSECRETKEYGOESHERE"
To get the value from the key use the following code
require('dotenv').config()
console.log(process.env)
For more information do check the link.
Using .gitignore file
.gitignore file is used to add those files which you don't want to commit to your git repository. In this case, you can create a file called.gitignore and in that file add the file names which should not be committed.
.gitignore
.DS_Store
secrets.txt
.env
#This is comments(# used for comments)
*.txt #this means all files with .txt extention will be ignored
To get the prebuild template for .gitignore files for nodeJs or any kind of project do check the repository down below.
https://github.com/github/gitignore

node.js require function not finding module

I have a server.js file that I downloaded from someone's website. The first line is: var express=require('express');
When I try to run this server with "node server.js" I get the following error: "Cannot find module 'express'." The express module is installed in the default node install location:
C:\Users\myname\node_modules\express\
I'm able to successfully run express by executing "node express.js" from the express install location in node_modules. I also tried copying over the express folder and file into my c:\node-testing\ directory where my server.js file is located but I still get the error. Any idea what the problem might be and how to fix?
You can set the NODE_PATH environment variable to tell nodejs to search other paths for globally installed modules that are not in the project directory.
See http://nodejs.org/api/modules.html#modules_loading_from_the_global_folders for details.
On Unix installations there are some built-in default locations, but on Windows, it appears you have to set this environment variable manually to support a global location.
FYI, if you want require to load a module from the project directory, then you have to use
require("./filename");
with the ./ in front of it. That's why it didn't work when you copied it to the project directory. node makes a distinction between loading from the project directory vs. loading from the node_modules directory below and thus requires a different syntax to specify which one you want. Express.js is also not a stand-alone module because it depends on a bunch of other modules so you could not copy only it. I'd recommend using the NODE_PATH option or install express into your project directory (it will end up in a node_modules sub-directory).
Node.js will only search for modules in from the current (and parent) directories. Unlike npm, Node has no concept of "global" modules.
You need to run npm install to install your modules into the directory containing your code.

How do I execute symbolic linked files in node.js?

I'm writing an application that contain a skeleton directory that would be copied to a new node project. Since I'm debugging the skeleton, I'm symlinking the file in my new folder,
so:
mainapp/skel/index.js
sampleapp/index.js -> ../mainapp/skel/index.js
sampleapp/package.json
sampleapp/node_modules/abc
index.js:
require('abc');
But running the sampleapp with:
node index.js
are not working because it seems to try to find the module in mainapp/skel/node_modules instead of sampleapp/node_modules. Any idea for a workaround?
You have two options -
Symlink node_modules directory in skel
Set NODE_PATH environment variable
export NODE_PATH=<path_to_sampleapp_node_modules>

Resources