Any ideas of a workaround without removing $?
Environment Variable: DB_PASS
.env file
DB_PASS = *^$php&!Bz
from '$env/static/private'
DB_PASS = *^&!Bz
Seemingly culprit character
$
env file
env console log
Got it, thanks to the GitHub community.
Vite uses dotenv-expand to expand variables out of the box.
Note that if you want to use $ inside your environment value, you have to escape it with \.
https://vitejs.dev/guide/env-and-mode.html#env-files
Related
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"
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.
I have some nodejs code running in a module somewhere. From this module, I would like to load a module located in a completely different place in the file system -- for example, given the path "/another/dir" and the module name "foo", I would like Node to act as if a module running in /another/dir had called require("foo"), rather than my own module.
My code is running here
/some/folder/node_modules/mine/my_module.js
I have the path "/another/dir/", the string "foo",
and want to load this module
/another/dir/node_modules/foo/index.js
In other words, the module documentation refers to the process "require(X) from module at path Y", and I would like to specify my own value for Y
Can this be accomplished? If so, how? If not, why not?
The simplest, is just to resolve the path into an absolute path, this will be the recommended approach for most if not all cases.
var path = require('path');
var basedir = '/another/dir';
var filename = 'foo'; // renamed from dirname
var filepath = path.join(basedir, 'node_modules', filename);
var imports = require(filepath);
If you really need to make require act as if it is in a different directory,
you can push the base directory to module.paths
module.paths.unshift('/another/dir/node_modules');
var imports = require('foo');
module.paths.shift();
module.paths can also be modified externally via the environment variable NODE_PATH, tho that would be the least recommended approach but this does apply it globally across all modules.
A symlink with npm link
To avoid problems or modify source code I would use npm link, from your example:
First:
cd /another/dir/node_modules/foo # go into the package directory
npm link # creates global link
This will create a global link for the foo module, on Linux you need root permissions to do this.
Then:
cd /some/folder/ # go into some other package directory.
npm link foo # link-install the package
/some/folder/package.json should contain foo as a dep, is not mandatory, without it you get an extraneous warning with npm ls:
"dependencies": {
[...]
"foo": "*"
}
No symlink with local NODE_PATH
You don't like symlinks ? You can still use NODE_PATH but locally instead setting a global variable as #rocketspacer suggested, because as he rightly stated, it's not recommended to use it globally.
Note: In any case I would use a User variable and not a System-wide variable, a co-worker could log with a different username on the same machine and still get a modified NODE_PATH.
But to do this locally for just one invocation on Linux you can simply call:
NODE_PATH=$NODE_PATH:/another/dir/node_modules npm start
It will use that NODE_PATH only for that invocation.
Same one time invocation on Windows:
#ECHO OFF
SET BASE_NODE_PATH=%NODE_PATH%
SET NODE_PATH=%BASE_NODE_PATH%;C:\another\dir\node_modules\
node index.js
SET NODE_PATH=%BASE_NODE_PATH%
And...
You could also use a local dep like:
"dependencies": {
"foo": "file:/another/dir/node_modules/foo"
}
But would require an npm install and it would copy the content of foo in the current package node_modules folder.
It's quite easy to achieve, just add absolute paths to module object
in your current script /some/folder/node_modules/mine/my_module.js add this
module.paths.push('/another/dir/node_modules');
//this must be absolute, like /Users/John/another/dir/node_modules
var foo = require('foo');
For demo, open node in terminal, and type module.paths, it will show all the path node will search for require, you just add your path
"require(X) from module at path Y"
Means calling require(X) from within a file located at path Y. So you practically can't change Y.
Although the above solutions work (modifying module.paths & requiring absolute path), you'll have to add those snippets to every file in your project where you need requiring the foreign modules.
Even more, modifying module.paths is not officially supported. So it's not guaranteed to work in future updates of node
Introducing NODE_PATH
NODE_PATH is an environment variable that is set to a colon-delimited list of absolute paths.
Within those absolute paths, Node.js will search for a module that matches your require statement when all else has failed (Having indexed node_modules up to File System Root and still no match was found)
It is officially supported, although not recommended as it goes against convention (Your co-worker may not be aware of the NODE_PATH usage as there is no descriptive way of telling that in your project itself)
Notes:
On Windows, NODE_PATH is delimited by semicolons instead of
colons.
Node doesn't look for node_modules within those paths like
its default behavior, so your paths should be exactly where you
contain needed modules. For example: "/another/dir/node_modules"
Detailed information can be found on NodeJs official document:
https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
For those of you who are unaware of what environment variables is and how to set them, I have a windows screenshot that will get you started
In simple lines, u can call your own folder as module :
For that we need: global and app-module-path module
here "App-module-path" is the module ,it enables you to add additional directories to the Node.js module search path And "global" is, anything that you attach to this object will b available everywhere in your app.
Now take a look at this snippet:
global.appBasePath = __dirname;
require('app-module-path').addPath(appBasePath);
__dirname is current running directory of node.You can give your own path here to search the path for module.
I'm using Webpack to bundle my JavaScript application. I saw a developer during a talk that had a line similar to this:
var foo = require('#/foo/bar');
OR
import '#/foo/bar';
What does the # symbol mean in this case? A co-worker thought that it might be a shortcut to node_modules, and it's extremely difficult to get good search results for this type of question due to ambiguity.
The # character has no special meaning in Webpack unless you configure it so. I suppose, it was a private npm module.
Google just released Closure, which is a compiler to minify JavaScript.
On the product site, it says "The Closure Compiler has also been integrated with Page Speed".
How do I use Page Speed to compile my web pages JavaScript with Closure?
(Or, is there a web site that I can simply paste in my JavaScript to have closure minify it?
For a single file it's simple
java -jar $path_to_jar/compiler.jar --js input_file.js \
--js_output_file output_file.js
For a multi-file project you can use calcdeps.py in combination with the compiler.jar
#!/bin/sh$
$CALCDEPS_PATH=/path/to_calcdeps #directory containing calcdeps.py
$JAR_PATH=/path/to_jar #directory containing compiler.jar
$CLOSURE_PATH=/path/to_closure #contains directory "closure"
$CALCDEPS_PATH/calcdeps.py --path $CLOSURE_PATH \
--path . \
--compiler_jar $JAR_PATH/compiler.jar \
--input main_project_file.js \
--output_mode compiled \
> compiled_project_file.js
That way compiler gives meaningful information about type errors, etc. Type errors can be caught at compile time because compiler.jar uses certain JSDoc comments for type information.
Extra compiler flags can be passed to calcdeps.py along with -f or --compiler_flags options
If you want to use advanced optimizations set
--compiler_flags "--compilation_level=ADVANCED_OPTIMIZATIONS"
notice the double quotes and the equal sign - had to use that format in bash
The Closure compiler is now available as a JavaScript application. No need for the Java dependency anymore
There are a few ways to integrate with it. I have done it as part of Rollup
ex:
import rollup from 'rollup';
import closure from 'rollup-plugin-closure-compiler-js';
export default {
entry: 'index.js',
dest: 'dist/build.js',
format: 'iife',
plugins: [
closure({
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT5',
compilationLevel: 'ADVANCED',
warningLevel: 'VERBOSE',
externs: [{src:`
var jQuery;
jQuery.fadeIn = function() {};
var ko;
ko.applyBindings = function(vm) {};
ko.computed = function(a,b) {};
ko.observable = function(a) {};
`}],
})
]
}
More info here:
http://www.syntaxsuccess.com/viewarticle/using-the-closure-compiler---advanced_optimizations
"Page Speed 1.4 Beta integrates the Closure Compiler to minify JavaScript files automatically. However, you will need to download and install the Page Speed Beta and Closure Compiler separately."
http://code.google.com/speed/page-speed/download.html
I haven't installed this version yet, but I'm fairly certain that Page Speed will present you with compiled code in its optimization recommendations.
It seems that Closure Compiler is integrated with Page Speed only for Windows.
Use the closure compiler with PHP (hosted via CURL or local via command line tool)
http://bohuco.net/blog/2009/11/google-closure-compiler-with-php/
If you need to compile multiple js files or if you would like to simplify compilation process, you may use kjscompiler: https://github.com/knyga/kjscompiler (based on google closure compiler)