I have used dotenv module to load environment modules into a config file. When I execute that file using node config.js I know that it has been loaded successfully(used console.log and saw output on the terminal). But when I am trying to run the entire project, it comes out as undefined in the console of the browser. Can anyone have a particular reason as to why this is happening?
running node config.js would run the file for sure and env variable will be available only during the life of this script. that's the reason you are not getting env variable in other places. try to inject require('dotenv').config() in the right place where you are consuming them. e.g build script. hope it helps.
Related
I have an NTVS (Node Tools for Visual Studio) project with Typescript.
The following statement doesn't compile:
import debug = require('debug')('MyApp');
The syntax error being
(TS) ';' expected
between the the two parenthesis ')('
Is it possible to use "debug" with TypeScript?
From the README, debug module is exporting a function that decorates console.error with your module name (MyApp). I'm guessing there are other ways, but I use:
import Debug from "debug";
const debug = Debug("MyApp");
// then to use
debug("Something happened");
And to print everything to the console, run your app with...
$ DEBUG=* node MyApp.js
The answers here did not work for me with more recent versions of Typescript. Here's how I got it working with proper import syntax in Typescript ^3.5.3:
Install Debug package and Typescript types for Debug (types only needed for dev)
npm install --save debug
npm install --save-dev #types/debug
Then in .ts files:
import Debug from "debug";
const debug = Debug("AppName");
Hope this helps someone else!
Remember that TypeScript is a super-set of javascript, so you can still also do this one-liner:
const debug = require('debug')('my-app:my-module');
Typescript seems to conclude that the debug constant here is of type 'any', and you lose all type safety, but with a package as simple as debug is, I think you will be OK...
Personally, I think 2 lines to instantiate debugging in every module is 1 line too many, so I continue to use this one-liner in my .ts files.
P.S. I like to use module tags so I can enable logging in just certain modules with DEBUG=my-app:my-module,my-app:some-other-module ts-node my-app or all my modules with DEBUG=my-app:* ...
The solution to debug not showing any logs in typescript is due to the reason that debug relies on the environment variables to decide how to show the logs
Solution
Make sure you have installed dotenv and its type definition file
npm install dotenv && npm install -D #types/dotenv
Then create a .env file at the root folder of your project add and this environment variable:
DEBUG = *
finally at the index file of your application. Configure dotenv to load the environment variables before any other task is run.
Its very important dotenv configuration is done at the top of the index file, before any other lines of code.
Add this two lines of code
import dotenv from "dotenv";
dotenv.config();
This should load the DEBUG environment variable required by debug to show output on the standard input.
Remember if you want to view logs defined in your files only and not other logs from other modules. Its better you define your application name as the namespace. That way you can filter the logs by the environment variable described above i.e.
const debug = debug("applicationName:other-more-information");
Then to view your debug logs alone just change the DEBUG varibale from * to applicationName:*
DEBUG = applicationName:*
For unix based os users you can try exporting this variable to enviroment variables directly (Though i have not tested this solution) export DEBUG=* - This method will only work for all process started on this shell
I've written a Casperjs script to do some scraping, however, after running into memory exhaustion issues, I've now written a node script to turn Phantom off and on via exec. I can run this with no issues locally, however when I deploy to heroku I get the following error
Error: Command failed: casperjs turnitoffandon.js
ERROR: stderr was:/bin/sh 1: casperjs: not found
I used the nodejs buildpack and have Phantom and Casper defined in my dependencies. In the heroku bash, running phantomjs --version returns 2.1.1 and casperjs --version returns 1.1.4.
Do I need to define where Casper is? If so how? I have set my PATH variable as /usr/local/bin:/usr/bin:/bin:/app/vendor/phantomjs/bin:/app/vendor/casperjs/bin:/node_module/casperjs/bin , like in this SO question
The issue actually had nothing to do with Heroku. As noted in this SO answer, using exec and providing any environmental variables in the options parameter replaces the entire set of environment variables. This includes the path, effectively overwriting any paths already specified to Heroku in the buildpack and npm modules.
You can create a copy of process.env and pass that along in the parameters in addition to other environmental parameters needed to be passed.
I created a command-line app that uses a .env file in conjunction with the dotenv NPM module to handle API credentials. So invoking the entry point like node main.js in the project dir works.
I'm now building a web-interface and invoking this application using the child_process module. The problem is that the environment variables are now mismatched. The command-line app sees process.env.APIKey as undefined and it crashes.
Now I think this is a unix-specific issue. How can I solve this problem (specifically, invoking main.js programmatically while retaining its own environment variables scope).
I want to run an index.js file as a part of my gulp process. I could add node index.js through something like gulp-shell as described in this thread (How to run bash commands in gulp?) but it seems there should be an easier way.
I figured there would be a plugin called gulp-node or something to do this but it doesn't exist. How do others do it without turning your initial script into a gulp plugin, which seems intrusive?
You can use var data = require('./path/to/index') to load/run the index.js module in your Gulp task and send any exported information to data. You can read more about require and Node.js modules here.
Having started to work on node.js recently, I am trying to use rename package. Successfully installed (npm install rename) package and sample code (residing in file test.js) is pasted below:
console.log('Starting to rename');
var rename = require('rename');
rename('a.js', 'b.js');
console.log('Rename done');
The file a.js resides in the same directory as test.js. The console shows the two debug messages correctly, but the file a.js is not renamed.
How can I enable debug on rename module (or for that matter any node module), so that I can debug it additional log messages?
Note: Other packages like find, mkdirp, etc are working fine.
You can enable debug by doing a DEBUG=* node app.js or passign env variable DEBUG with wildcard * . It would enable debug for all modules, provided the module you wish to debug is actually using debug.
Hardcore way of doing is to make local changes in the module.