Node hot reload - node.js

i wanted to implement an hot-reload feature in my app.
Basically when i change a file that returns a value i want it to be working on my running application.
This involves some require.cache knowledge that maybe i got wrong,
i've found a small repo that i thought it could help me with no chance.
The repo is clear-module and i prepared a small example on top of it
git clone git#bitbucket.org:giggioz/test-clear-module.git
install the modules with
npm i
and then run it with
node index.js
This code prints every 500ms a value.
This value comes from a delegate.
My Issue
If i change the value returned by get-points.js while the code is running it does not do anything... i would expect a change, right?
Thanks for your help!

Related

Phaser 3 - Images not showing

After searching for a bit and finding others having somewhat of the same problem, but no solution for me, I figured it was my turn to seek help.
I have npm along with parceljs setup for webserving (I'm new to these but I'm quite confident it's working as you can see some output in the image I've posted)
I have the typical problem of images not displaying.
Status code of the image is returning 304 OK, and is it normal to not see the the image listed in the directory under the 'Sources' tab in Developer tools?...
The issue is with the way you are loading the image urls as hard-coded strings, for example, you wrote this:
this.load.image('sz','./src/assets/gfx/sz0001.png`)
The way this is written, parcel doesn't know that this string is actually a reference to an image file, so it doesn't know to copy over the image into your output folder and ensure that the path ends up matching the actual output location.
The way parcel works, if you want to include an asset (like an image) into your bundle and reference it by URL in some other javascript context (like the this.load.image function), you should use an import statement or a URL constructor (see docs).
So you would instead write:
import myImageUrl from './src/assets/gfx/sz0001.png`;
// 'myImageUrl' will be a string that points to the location of the image
// This will also tell parcel that sz0001.png is a dependency of this project
// so that it can take care of copying it over to the dist output.
this.load.image('sz',myImageUrl)
Another tip is that the template you referenced uses parcel-bundler which the old, unmaintained 1.x version of parcel. I'd recommend upgrading your project to parcel 2, which on npm is simply parcel (see migration guide).
The green box, is shown if the asset is not found, or some other error.
Since I'm not 100% sure how parcel works and I don't know your parcel configuration and which parcel plugins you are using (I'm assuming parcel can be configured).
You could solve the problem by: manually copying the assets folder from src/ to dist/, than it should work. (atleast this work for me, when using this parcel template, to test the issue)
Maybe a parcel - plugin like parcel-plugin-static-files-copy, could also solve the problem, and/or be useful to keep the static files up-to-date.

Log statements in Node Module not getting printed

I am a new to Node JS. I have included a module via npm install '<module_name>. It was built correctly and there was no errors. Now, I wanted to debug it, so I placed a few console.log('some text') in code blocks of the module to see if the code by passes that line. Anyway, none of the log statements were displayed.
I am wondering if I have to compile or something the modules after adding the log staements. Am I missing something here.
Your console.log statements are not being run, this could be caused by many things.
Assuming you have added the console.log statements to the module code in the node_modules directory of your app..
does the module have src and dist directories and you have not edited the code that is actually being run? (this relates to needing to recompile, but editing the actual code that the module is running will be quicker and easier)
if this is in a server or long running script it will need to be restarted to load the changes
is this in a browser which might be caching the code (turn off browser cache)
is the code where you added the log statements actually being hit?
I would make sure I had a console.log statement in a part of the code guaranteed to be hit, just as a sanity check.
For anyone coming here in the future, try console.error instead of console.log. For some decided reason or another, log was being overriding by the library I was monkey fixing. Took me way too long to find the culprit.

Trying to launch my node.js program with npm but nothing happens

I am trying to make this slack bot run : https://github.com/lmammino/norrisbot
I am not very skilled with npm and node yet, but I follow his instructions and try to run the bot with the help of the npm start command.
Here's the output I get :
F:\norrisbot>npm start
> norrisbot#1.0.5 start F:\norrisbot
> node bin/bot.js
F:\norrisbot>
No error, but nothing happens either in the console or the slack general channel...
By the way I set up my BOT_API_KEY variable correctly (with the token.js method)
By your command prompt it's clear you're running in Windows. The operations for running Node properly in Windows are different in several ways from Mac/Linux, and a LOT (most?) of developers don't address these because they're on Mac/Linux themselves. Path formats, file locations, how you expose environment variables, and all sorts of things are different in Win.
Try hand-editing bin/bot.js in your locally cloned copy of the repo. Find this line at the end of the file:
norrisbot.run();
Change it to read as follows:
console.log('Running Norris Bot');
norrisbot.run();
console.log('Ran Norris Bot');
I bet you will find that either NEITHER of these lines gets printed, or only one does.
If NEITHER line gets printed, the issue is with the npm command improperly formatting the path to the executable script for Windows users. In that case, try running it as (make sure NodeJS is in your PATH):
node bin/bot.js
If only the FIRST line gets printed, there is almost certainly a bug elsewhere in the module itself. I didn't evaluate all of its code, and I'm not on Windows myself at the moment - I just use it often enough to be aware of its differences. But either way it will get you started on finding the issue, and if it's truly a bug, you can pursue the bug report I see you've already filed in Github.

The easiest way to develop the Yeoman generator's template itself

Sample situation
I have my own Yeoman generator, which has a folder with "template" of the resulting project.
The generator takes some information from user, interpolates the "template" with the information and then outputs a simple working project.
I want to ensure the "template" is actually working, at least in one positive scenario if not with all combination of inputs. I can write integration tests (which will run the generator with some data and then try to run the resulting code and verify whether all works as expected), but still, that's sometimes too much work and it's inconvenient for trial and error kind of development or some prototyping.
Question
Is there an easy way how to work with the "template" itself, how to run it or use it locally, manually, without the need to run the generator first every time I change a single letter in files of the "template"?
Maybe some sort of build step, which would run the generator for me with some preset data? Is there anything ready in form of npm module? Does a best practice exist?
After running the integration test, you can spawn some commands in the generated project folder and see if those are passing fine.
So far, the best solution I found is to create a script, which:
Creates a temporary sandbox directory.
Performs npm link
Alters the PATH so it does not contain .bin of your local node_modules (this is needed to prevent locally installed Yeoman take precedence over the global one when the script is ran e.g. as npm run develop).
Sets an environment value NON_INTERACTIVE to something truthy.
Runs yo <your generator> in the sandbox directory.
Runs npm start in the sandbox directory to run the freshly generated server code.
Change your generator so it is able to automatically provide some dummy default values for required prompts without default values if process.env.NON_INTERACTIVE is truthy.
Then run the script as:
$ nodemon --watch <directory with your template> --exec <path to your script> --ext js
It's slow, but it works. This way you can develop the template itself and avoid filling the generator every time you need to try out something.

Gulp task finishes but never ends

NOTE: Here is an example repo with the problem.
When I run ./gulp js, the process works (creates the expected files on the file system), but the task never completes... just hangs indefinitely:
ss http://zc.d.pr/4C9U/3GG90rpz+
I figure I'm not returning something somewhere, or invoking a callback correctly, but after hours of tinkering, head-banging, and Googling, I haven't found a solution.
Can someone help me out here?
If it makes a difference, I'm currently using node v4.1.0. All other dependencies and versions are in the example repo linked above.
EDIT: Original inspiration for this gulp recipe came from https://truongtx.me/2015/06/07/gulp-with-browserify-and-watchify-updated/
However, I couldn't get transform to work as that author suggested, which led me to https://github.com/substack/node-browserify/issues/1198#issuecomment-89948202
Of course—as it always happens—I think of something new to try just after I post to SO and it appears to work.
doh http://zc.d.pr/11uMa/5gghjbCx+
You can see my full changeset here: https://github.com/neezer/gulp-browserify-hanging-task/commit/8156e182c04c2e76c5739e31f5a6e417dda01b70
TL;DR Basically I tried the suggestion in the last comment on the aforementioned issue from my question, where I pass the file object itself to browserify instead of the file path, and lo-and-behold, the task finishes now.
I don't pretend to know why that fixed the issue, so if anyone would like to explain, I'd love to learn. ;)

Resources