error with discord.js when reading .env variables - javascript

I'm using discord.js to learn java script, but I'm having a problem with variables stored in .env
apparently they are not being read correctly; and honestly, I don't know if I'm doing something wrong because I don't really know how it works.
my variable in .env is written like this:
TOKEN = xxXxxXXXXxXXXxXXXxXXxXXX
LOG_CHANNEL = 1071616980039249920
i know dotenv is working fine because this line works perfectly:
client.login(process.env.TOKEN)
while I have this line in index.js:
client.channels.cache.get(process.env.LOG_CHANNEL).send({
embeds: [
// random embed here
]
})
and i receive this error:
client.channels.cache.get(process.env.LOG_CHANNEL).send({
^
TypeError: Cannot read properties of undefined (reading 'send')
but it works perfectly when i change it to:
client.channels.cache.get('1071616980039249920').send({
embeds: [
// random embed here
]
})
I tried changing the way the code is written in the .env by adding ` to the variable declaration, but apparently it didn't make any difference.
I'm looking for a way to make this work, as this snippet in index.js recurs frequently and I plan to change the log channel a few times.
Also, I would love a basic explanation of how these declarations work in the .env: are they all considered strings, even without quotes?

Environment variables from .env file are not loaded into Node by default. You can use npm package dotenv to load the variables from the file.
To do so, install the package:
npm install dotenv --save
Then, in your main file, at the beginning, add the following code:
require('dotenv').config();
After this line of code is called, your variables from .env file are available as properties of process.env, i.e. process.env.LOG_CHANNEL. They are of string type. If you want to use them as numbers, you need to convert them in your code.
Also, you might need to keep your variables in .env without additional spaces and with quotes:
LOG_CHANNEL="1071616980039249920"

Related

Why I get UNDEFINED from ENVIRONMENT VARIABLE in REACT App?

Unfortunately, it's a pretty simple question, but I can't find the answer.
Don't have an access to environment variables in some file.
I use create-react-app (^16.11.0)
file .env in the root folder near package.json
variable declared as REACT_APP_API=http://localhost:4000
call variable as process.env.REACT_APP_API
the server has been restarted many times
I have an access to the variable in some folder but in another don't.
below link to files structure
workflow structure
Thanks a lot!
THE PROBLEM WAS FOUND!
As often happens, the reason is funny.
When typing process.env.REACT_APP_API, my IDE automaticlly added an import at the top of the file
import process from "process";
That's why i can't access to process.env. in one specific file.
Import removed, problem solved

Environment Variables in NextJS are Undefined

I am using the next-auth library which requires the use of environment variables as follows:
Providers.GitHub({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
However, when I test out next-auth it is not working and it seems to me the reason why is because those variables are not loading properly. As such, I did a little test to see if I can access environment variables in my app and the test showed that I cannot.
This is how the test went:
// .env.local (root level)
NEXT_PUBLIC_ENV_LOCAL_VARIABLE="public_variable_from_env_local"
I then add the following code to my site:
<h2>test one start</h2>
{process.env.NEXT_PUBLIC_TEST_ONE}
<h2>test one end</h2>
<Spacer />
<h2>test two start</h2>
{process.env.TEST_TWO}
<h2>test two end</h2>
In this case, test one start shows up and test one end shows up, but the environmental variable does NOT show up. The same is true for test two start and test two end.
I did a similar test with console.log - namely:
console.log("test one", process.env.NEXT_PUBLIC_TEST_ONE)
console.log("test two", process.env.TEST_TWO)
That turns up as follows:
test one undefined
test two undefined
In short, for whatever reason I seem unable to load environment variables in my nextjs app. Not from next-auth, not in the code and not in a console.log. The variables are undefined and I do not know why.
Any ideas?
Thanks.
.env.* files are loaded when server starts. Hence, any modification in them is not applied unless you restart your dev/prod server.
Just halt the process (Ctrl+C/Z) and re-run next dev/next start to make them work. Note that you may also need to re-build the project (next build) before starting in production mode if you are using them in pages that are statically generated.
To be clear, according to the most recent docs, you are supposed to
place the variables in an .env.local file
then config your next.config.js file so it includes an env config
referencing your variables like this:
module.exports = {
env: {
secretKey: process.env.your_secret_here,
},
}
then you can call the variables on the client-side using the typical process.env.secret_here syntax. I may be mistaken but I do believe the NEXT_PUBLIC prefix exposes the env variables to the client-side and should ONLY be done in a dev environment to avoid security issues.
Searching the next.js documentation I found this:
// file: next.config.js
module.exports = {
env: {
customKey: 'my-value',
},
}
// Now you can access process.env.customKey
// For example, the following line:
// return <h1>The value of customKey is: {process.env.customKey}</h1>
// Will end up being:
// return <h1>The value of customKey is: {'my-value'}</h1>
So it seems, if you are using next.js version at or before 9.4 you place env variables into next.config.js
They also include one warning:
Next.js will replace process.env.customKey with 'my-value' at build time. Trying to destructure process.env variables won't work due to the nature of webpack DefinePlugin.
If you are using version 9.4+ the documentation says you can use .env.local files and prefix env variables with NEXT_PUBLIC_ to have them exposed in the browser.
Of note with this method is: In order to keep server-only secrets safe, Next.js replaces process.env.* with the correct values at build time. This means that process.env is not a standard JavaScript object, so you’re not able to use object destructuring.

How do I use require.js?

I have a folder that contains another folder and a file "server.js", the file is the node file and the folder contains HTML and client.js. server.js has a variable that holds the port for the website, and I want to access that variable to use it in the client.js, but it keeps erroring, I tried using import statement in the client.js but it gives "Uncaught SyntaxError: Cannot use import statement outside a module". I tried using require("server.js") but learned that client-side js doesn't support require() function. I searched and found that there's a library called RequireJS, while there are some tutorials to use it, they all use the same example and it always gives the same error no matter how much I changed the code, so please help me with a detailed tutorial on how to use RequireJS.

How to pass a parameter to --world-parameters or npm run command to be consumed by scripts in package

I know that this question could be a duplicate of many similar existing questions however, still want to ask more precisely for help in the scenario I want to understand:
I am using this repo in my example and I have following script block in my package.json
I need to pass one parameter from the command line to identify the environment in which I wish to test, for example something like:
-- testEnv=staging
How can I update the following script blcok to accomodate this change.
I have already tried to set different configurations for world parameter like this:
--world-parameters \"{\\\"environment\\\": \\\"Dev\\\"}\"
however it is now confusing to maintain various version of world parameter configs hence looking to use command line to send variable values through.
"scripts": {
"test-chrome": "./node_modules/.bin/cucumber-js.cmd --tags #Run --format json:./testlab/support/reports/report.json",
}
TestCafe allows you to use environment variables, and have a config.json file to store the baseUrl:
So, you could do
export testEnv=staging
npm run test-chrome
Then enter that value as part of your config file.
{
baseUrl: process.env.testEnv
}
Or, if you want a default baseUrl, you could have a helper class that just returns const targetUrl = process.env.testEnv || config.baseUrl.
Starting with version 1.20.0, TestCafe offers a way to specify the base url for test pages in all interfaces:
CLI
Program API runner.run({baseUrl})
Config file

Heroku process.env not showing config vars

I have a react app in a Heroku pipeline. Simply staging -> production.
In staging => Settings I have some config vars set up for the environment. However, when I console.log(process.env) I do not see them.
Am I supposed to be able to access these config vars in this manner? From what I've read, I should be able to access them from process.env. No?
I am using the following buildpack: https://github.com/mars/create-react-app-buildpack.git
Apparently you MUST name the config vars starting with REACT_APP_ and then you can access them via process.env.
This is all clearly outlined in the buildpack's documentation. I was just too dumb to actually check there :/
Append your Config Var name with the string 'React_App_' at the beginning (like React_App_Var_Name) and use this name in your ReactJs app. If this string is not appended and used, then it will return undefined.
For deploying production build, follow the steps mentioned in this link.

Categories