How to use Node's debug module (Windows)? - windows

I am trying to figure out what is wrong with my sessions (using express-session), and I found that it uses the debug module. However, I can't seem to enable the debug messages. It says, debugging needs to be enabled through the DEBUG environment variable, but I can't seem to get it to run.
The tutorial in the README has this picture:
Under Windows I get "DEBUG is not a command for the command-line".
So I tried setting the environment variable explicitly using:
process.env.DEBUG = "*";
and still nothing.
What am I doing wrong?

As Traveling Tech Guy suggested in the comment, in a Windows command prompt, the proper syntax is:
set DEBUG=* & npm start
Obviously you can replace npm start with whatever command you need to launch your Node.js app. Just be sure to use the set command and don't forget the & between that command and the one launching your Node.js app!

If you are used to powershell, I recommend this setup in your package.json, then you can just run npm start so you don't type all that out every time.
"scripts": {
"start": "#powershell $env:DEBUG='*,-express:router*' ; node app.js"
},

Install debug package with npm inside the node application
npm install debug
Open a powershell and then
$Env:DEBUG="name_to_call"
node path_to_js_to_execute.js
Inside your pgm
var debug = require('debug')('name_to_call');

Firstly you need to install the debug module using
"npm install debug --save"
you will see that the following lane has been added to you package.json (which has all the npm modules charged to your proyect)
then you need to add this to import the module to the file in which you want to run the debug
var debug = require('debug')('name_to_call');
now we have to just put the message we want to write, for example hello
var debug = require('debug')('name_to_call');
debug('Hello');
(try pasting the code above directly to a file)
Now we just need to start the windows server with DEBUG, to do so we are going to use the npm package cross-env that will make easier to set an ENV variable across any operating system (platform agnostic)
npm install cross-env
Change the package.json and add the following under the scripts section
"start-server": "cross-env DEBUG=name_to_call node server.js"
Now to start the server just run the following in a command line (from the directory in which your project is) and you are good to go
npm run start-server

In Babun a windows shell, I run,
npm
npm install debug --save
babun
DEBUG=http node app
app.js
var app = express();
var debug = require('debug')('http');
var http = require('http').Server(app);
var server = http.listen(app.get('port'), function() {
debug('listening on port ' + server.address().port);
});
works like a charme for me.

Windows Command:
set DEBUG=* & node ./app.js
PowerShell:
$env:DEBUG='*'; node app.js
Terminal/WSL (Ubuntu):
DEBUG=* node ./app.js

Related

Suddenly, NPM script variables no longer work

I use package.json variables like this in NPM scripts:
// package.json
{
"version": "0.12.1",
"scripts": {
"get-version": "echo %npm_package_version%"
}
}
npm run get-version currently echoes %npm_package_version% instead of 0.12.1. In the past, the scripts worked without any problems. Suddenly only the variable name comes back. With multiple repositories. I run Windows 10 2004 and NodeJS v15.4.0.
Was there a change for NPM scripts in Node.js 15? Is it a bug or a feature?
UPDATE: Failure to expand environment variables on Windows appears to be a recent high-priority known bug in the npm CLI.
Because this is npm#7 specific, until a fix is released, you can downgrade to npm#6.
ORIGINAL ANSWER:
The easiest solution for the specific case in this question is to use node.
"get-version": "node -p process.env.npm_package_version"
This will work on every platform that Node.js supports.
If you need a more general solution and don't want to rewrite a bunch of scripts to use node, you can try cross-var as mentioned by #RobC in the comments.
As for the source of the problem, perhaps you are running under the Windows bash shell, in which case you can use this:
"get-version": "echo $npm_package_version"
That won't work for non-bash Windows environments though.
I found simple hack which is working perfect in my case,
Specifically in your use case
// package.json
{
"version": "0.12.1",
"scripts": {
"get-version": "node -e \"console.log(process.env.npm_package_version)\""
}
}
Usage
npm run get-version
However you want to pass arguments.
// package.json
{
"scripts": {
"get-argument": "node -e \"console.log('your argument:', process.argv[1] )\"",
}
}
Test example
npm run get-argument hello_world
Default values are a great way to handle undefined values. We use a predefined value instead. Inside our NPM script we can achieve that by using the following syntax;
{
"version": "0.12.1",
"scripts": {
"get-version": "echo ${npm_package_version:0.99}"
}
}
And of course, running npm from a bash prompt might help. I guess running from a Cmd/Powershell "could work" but I would be careful about that.
FYI - A related change in Version 7 if you are using the Package config variables:
The variable name changed from npm_package_config_customFooVar in V6 to npm_config_customFooVar in V7
Delineate these appropriate (as below) to the environment (Windows bash linux etc) being used. or Use lib like cross-var.
Package.json
{
"config": {
"customFooVar": "bar",
"env": "development"
},
"scripts": {
"get-var": "echo using env1 $npm_config_customFooVar OR env2 %npm_config_customFooVar%"
"build": "npm config set myAppName:env"
"postbuild": "cross-var ng build --configuration=$npm_config_env && cross-var node myOtherBuildSript.js $npm_config_env"
}
}
e.g. npm-cli call (note space after --) as this is passed to the script. Not to npm itself.
npm run build -- production
pass args from package.json to cli
echo %npm_package_version%
This solution allowed me to use the npm_package_version variable in both Windows and Unix:
Install run-script-os as a dev dependency. Then in your package.json the variable can be used:
"scripts": {
...
"postversion": "yarn postversion-wrapper",
"postversion-wrapper": "run-script-os",
"postversion-wrapper:windows": "echo %npm_package_version%",
"postversion-wrapper:nix": "echo $npm_package_version"
}

Unable to debug firebase functions

I am trying to debug my js code the runs on firebase functions.
My steps were:
install from functions
npm install --save #google-cloud/debug-agent
added index.js:
require('#google/cloud-debug').start();
when I tryed to run
firebase deploy --only functions
got an error :
Error: Error parsing triggers: Cannot find module '#google/cloud-debu
g'
Try running "npm install" in your functions directory before deployin
try: ndb firebase serve
debugger breakpoints are hit with stack traces visible, note it's a little slow so give the debugger time to instrument the child processes
Additionally I was able to debug cloud functions in isolation using (caps for removed values):
GCLOUD_PROJECT=THE-FIREBASE-PROJECT node --inspect-brk /path/to/functions-framework --target FUNCTION-NAME --port=5000
where functions-framework simply expands to the full path for the installed functions-framework (global in my case) and the working directory contains the target index.js for functions.
Alternately when or where the FIREBASE_CONFIG is needed try this format adjusted to fit:
FIREBASE_CONFIG="{\"databaseURL\":\"https://YOUR-FIREBASE-PROJECT.firebaseio.com\",\"storageBucket\":\"YOUR-FIREBASE-PROJECT.appspot.com\",\"projectId\":\"YOUR-FIREBASE-PROJECT\"}
https://github.com/GoogleChromeLabs/ndb
https://cloud.google.com/functions/docs/functions-framework
https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/15
The addition to index.js should be:
require('#google-cloud/debug-agent').start();
or better:
require('#google-cloud/debug-agent').start({ allowExpressions: true });
We recently renamed the module, and it is possible that the instructions you are following are partially out of date. Can you point us to the instructions you have been following?
Also note that support for debugging cloud functions is experimental at this point. There are certain cases (dependent on the traffic to the function) where you function may finish before the debug-agent has a chance to initialize/register. We're currently looking into how to address this.

how to breakpoint to webpack in intellij IDEA?

I want to breakpoint to webpack Source Code in Intellij IDEA 2016.2 for Mac.
it tips:
To debug "build-distributor" script, make sure $NODE_DEBUG_OPTION string is specified as the first argument for node command you'd like to debug.
For example:
{ "start": "node $NODE_DEBUG_OPTION server.js" }
but,where is add this code when debugging webpack?
Today I had the same problem and found the place where to put the NODE_DEBUG_OPTION. You have to change or create a new npm script which points to the javascript file. For example, I wanted to set breakpoints in an webpack plugin so my original npm script looked like
"scripts": {
"build": "webpack"
},
my new script with the NODE_DEBUG_OPTION looks like
"scripts": {
"build": "node %NODE_DEBUG_OPTION% ./node_modules/webpack/bin/webpack.js"
},
I'm working on an Windows Machine. Thats the reason why my NODE_DEBUG_OPTION is between two "%" and yours have an "$" in front of.

How to use Firebase-tools on Windows?

I want to get started with firebase on a Windows machine but I don't understand the getting started instructions on https://www.firebase.com/docs/web/quickstart.html.
I created a .html file with the following content (copied from the instruction on that page). That works, info is added to the database and retrieved from the database. However I'm lost on Linux like instructions like $ npm install -g firebase-tools on that page.
I installed nodejs following the link to nodejs.org on https://www.firebase.com/docs/hosting/quickstart.html
If I execute the above command (without the linux $-prompt) in the node.js screen I get the following error message npm should be run outside of the node repl, in your normal shell.
(Press Control-D to exit.)
So then what?
<html>
<head>
<script src="https://cdn.firebase.com/js/client/2.2.1/firebase.js"></script>
</head>
<body>
<script>
var myFirebaseRef = new Firebase("https://torrid-inferno-6000.firebaseio.com/");
myFirebaseRef.set({
title: "Hello!",
author: "Firebase",
location: {
city: "San Francisco",
state: "California",
zip: 94103
}
});
myFirebaseRef.child("location/city").on("value", function(snapshot) {
alert(snapshot.val()); // Alerts "San Francisco"
});
</script>
</body>
</html>
Basically i had to open command prompt and switch to C:\Program Files\nodejs, where file npm is located), then npm commands can be executed according to instructions. Later on when firebase is installed perform a restart for the changes to PATH environment variable to take effect. After that the firebase (init, deploy, ...) command can be used to deploy a site.
Firepit is a new CLI tool built for Windows that attempts to be the native firebase-tools
Firepit is a standalone, portable version of the Firebase CLI which has no depedencies (including Node.js). Download, click, and immediately get access to both firebase and npm commands.
Worth checking out https://github.com/abehaskins/firepit

How do use node-qunit?

The info on this page seems less-than-forth-coming -- https://github.com/kof/node-qunit. I've got a setup where I installed nodejs and installed the node-quit module. I have test runner and executed the command node /path/to/runner.js. Below is an example of my setup. Any ideas or examples on how to do this or maybe I'm using it wrong. I previous ran qunit tests using Rhino and EnvJs without any issues but I figured I try nodejs since I using it for other things and the packaging system can be scripted in my build. Maybe I missing an option to node to include Qunit or some environment variable not set -- that would make sense.
File Structure
node/
public/
js/
main.js
tests/
js/
testrunner.js
tests.js
Installation
cd node
npm install qunit
This will now update the file structure.
node/
node_modules/
qunit/
tests/js/testrunner.js
var runner = require("../../node/node_modules/qunit");
runner.run({
code : "/full/path/to/public/js/main.js",
tests : "/full/path/to/tests/js/tests.js"
});
tests/js/tests.js
test("Hello World", function() {
ok(true);
});
Command
node tests/js/testrunner.js
It appears that you need to use full paths to the main.js and tests.js files and also include a relative path to the qunit module. I updated the code above as an example for others.

Resources