Parsing errors appear randomly and temporarily - node.js

I am working on a small react app and every so often after I save a file, the code won't compile due to some parsing error on a line that I didn't change and had not been previously problematic. A workaround I have found is to delete a random semicolon, save, put the semicolon back and save again which then allows my app to recompile without issue.
I've pasted some code below that now apparently has errors. I've also pasted an image of my error message in the browser. Seems to be related to my destructuring perhaps?
I have tried running npm install again but that didn't solve the issue. I am running node version v6.11.3 and react version 16.7.0,
I would like to solve this mysterious issue -- has anyone else have this happen to them?
maybeRenderCalendar() {
const { openCalendar } = this.state;
if (!openCalendar) {
return null;
}
return (
<Calendar />
);
}

Related

Nodegui trying to retrieve wifi networks not being executed

I'm trying to get a list of currently connected wifi SSID's, for which I use the node-wifi library, I tried with a very basic block of code, which is working in regular node. However even after doing a rebuild of the dependencies with qode I don't get it to work in nodegui, I started from the react-node-gui starter and added a button which calls this code:
import wifi from "node-wifi"
export const getCurrentWifiConnection = async () => {
wifi.init({iface: null})
console.log("lets get the networks...")
let currentConnections
try{
currentConnections = await wifi.getCurrentConnections()
console.log(currentConnections)
} catch(e) {
console.log(e)
}
return currentConnections
}
anyone who has an idea of what's going on?
I don't see anything from my console.log with the current-connections or from the error (doesn't matter if I use console.log or console.error) the "let's get the networks" log message gets printed though, so the function is getting called.
I've tested this code on node-16 and it's running perfectly if nodegui isn't wrapped around it, I know nodegui is using it's own customized node runtime with extra bindings for QT events, so I believe this has something to do with the qode.

Cannot set property 'avatar' of undefined at Member.set [as avatar] - Discord Bot

I haven't used my Discord recently but the last time I ran it, it still worked perfectly. However, I keep getting this error Cannot set property 'avatar' of undefined at Member.set [as avatar] these days.
Can someone help me?
I was struggling with this issue and eventually figured it out. It's because the 'this' keyword is getting hijacked somehow in the code within the Object.defineProperty call inside the Member constructor(line 2606 in my version of index.js, but I've made a few other fixes already so yours is probably different). I was able to fix it by caching off the 'this' reference into a private member and referencing that instead. It feels hacky but it works. Like so:
function Member(client, server, data) {
copyKeys(data, this, ['user', 'joined_at',]);
this.id = data.user.id;
this.joined_at = Date.parse(data.joined_at);
this.color = colorFromRole(server, this);
var tempThis = this;
['username', 'discriminator', 'bot', 'avatar', 'game'].forEach(function(k) {
if (k in Member.prototype) return;
Object.defineProperty(Member.prototype, k, {
get: function() { return client.users[tempThis.id][k]; },
set: function(v) { client.users[tempThis.id][k] = v; },
enumerable: true,
});
});
}
I just experienced the same error with my own bot completely out of the blue. After some investigation, I checked the code at DiscordClient.handleWSMessage (on my error it was showing at index.js:1871:31 as opposed to index.js:1891:31, however I'm not sure if it's a matter of having different versions of discord.io installed) - in any case, the error seemed to be stemming from the Event switch statement responding to a GUILD_CREATE event - this may be different for you:
case "GUILD_CREATE":
/*The lib will attempt to create the server using the response from the
REST API, if the user using the lib creates the server. There are missing keys, however.
So we still need this GUILD_CREATE event to fill in the blanks.
If It's not our created server, then there will be no server with that ID in the cache,
So go ahead and create one.*/
client.servers[_data.id] = new Server(client, _data);
return emit(client, message, client.servers[_data.id]);
I don't understand why GUILD_CREATE events are being received, my bot has never been programmed to handle these, however commenting out the executable lines in the switch case above and replacing them with an empty return statement seemed to stop the error occurring, and my bot stayed connected (so far, I've only been testing for a few minutes).
Might not work for everyone, but on my computer this issue was resolved by saving and only running after I saved. Slightly annoying but it worked!

Retrieve file contents during Gatsby build

I need to pull in the contents of a program source file for display in a page generated by Gatsby. I've got everything wired up to the point where I should be able to call
// my-fancy-template.tsx
import { readFileSync } from "fs";
// ...
const fileContents = readFileSync("./my/relative/file/path.cs");
However, on running either gatsby develop or gatsby build, I'm getting the following error
This dependency was not found:
⠀
* fs in ./src/templates/my-fancy-template.tsx
⠀
To install it, you can run: npm install --save fs
However, all the documentation would suggest that this module is native to Node unless it is being run on the browser. I'm not overly familiar with Node yet, but given that gatsby build also fails (this command does not even start a local server), I'd be a little surprised if this was the problem.
I even tried this from a new test site (gatsby new test) to the same effect.
I found this in the sidebar and gave that a shot, but it appears it just declared that fs was available; it didn't actually provide fs.
It then struck me that while Gatsby creates the pages at build-time, it may not render those pages until they're needed. This may be a faulty assessment, but it ultimately led to the solution I needed:
You'll need to add the file contents to a field on File (assuming you're using gatsby-source-filesystem) during exports.onCreateNode in gatsby-node.js. You can do this via the usual means:
if (node.internal.type === `File`) {
fs.readFile(node.absolutePath, undefined, (_err, buf) => {
createNodeField({ node, name: `contents`, value: buf.toString()});
});
}
You can then access this field in your query inside my-fancy-template.tsx:
{
allFile {
nodes {
fields { content }
}
}
}
From there, you're free to use fields.content inside each element of allFile.nodes. (This of course also applies to file query methods.)
Naturally, I'd be ecstatic if someone has a more elegant solution :-)

Phaser problem: loading images imported from files as base64 data

I have trouble loading image(s) as Base64 data. In the following code (partially borrowed from here), the 'loading' function this.textures.addBase64 is used:
import bgSrc from '../assets/img/back.png';
...
create() {
this.numToLoad = 1;
this.textures.on('addtexture', () => {
console.log('another image loaded')
this.numToLoad--;
if (this.numToLoad < 1) this.createNext();
})
this.textures.addBase64('background', bgSrc);
...
}
createNext() {
//...use the images
}
However, the 'addtexture' event never fires. And the image, loaded this way, never loads, no matter how long I wait.
But everything goes fine if I use direct Base64 code, like const bgSrc = 'data:image/png;base64, blablablah' instead of import bgSrc from '../assets/img/back.png'. So the problem is in importing the image.
I use phaser 3 webpack in this project. plus url-loader. Maybe the url-loader is incorrectly set up? I am not sure if the url-loader is even needed for performing npm run start.
Well, it was indeed an incorrect setup of the url-loader. (I used one more plugin in the webpack, and they didn't work well together.)
So, the code above is OK after all.

Babel / Node / Relay / Webpack cache?

Generally:
Do these four systems have caches? And if so, what is the method for clearing each?
Specifically:
Having trouble with a react app we are developing. Seemingly sporadically we get the following error when developing locally:
"Invariant Violation: RelayQL: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used verbatim as `Relay.QL`."
Am yet to notice any particular reason why/when this starts happening.
I finally found a hack solution which involved me going into the referenced component file referenced (further in the error msg, unshown) and deleting the RelayQL fragment inside e.g.
export default Relay.createContainer(PinterestShare, {
fragments: {
resource: () => Relay.QL`
fragment on Resource {
id
title
files {
type
images {
medium { url width }
}
}
}
`
}
});
to
export default Relay.createContainer(PinterestShare, {
fragments: {
resource: () => Relay.QL`
`
}
});
I then save, and reboot the app. It crashes, obviously, as the fragment is malformed. So I undo the change back to the original, and reboot the app again. Now, the original error is fixed, despite no code actually changing.
So what happened? Somehow doing this process is flushing some cache? Is this in node, webpack, relay, or babel? I've tried rebooting my machine inbetween, as well as killall node, neither which work, which implies to me it is not RAM based...
The annoying part now is I am having to do this for all of my individual component files. Surely there must be a way to purge this mystery cache enmass for the whole app?

Resources