How do I solve ReferenceError: regeneratorRuntime is not defined - node.js

I've seen that issue a lot here but none of the solutions worked for me. I'm using NodeJS and had no issue until I changed the project's directory.
Since then I can't get my code to work...
I've included:
import "#babel/polyfill"
I'm using async / await and this is clearly what's causing the issue:
async function process_data(post) {
// my_code
}
If I write the code like that:
const test = async function process_data(post) {
// my code
}
That's working but I can no longer call the process_data method on its own with the parameter (or else, I don't know how do it).
Any idea how I can get that to work?

By tweaking the code I found the answer:
const my_func = async function process_data(post) {
// my code
}
var res = my_func(post_var);

Related

tsnode with new AsyncFunction gives error

const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
const fn = new AsyncFunction('a','await blah')
So I'm trying to create a function using new AsyncFunction
This is working perfectly on the server. But when I do tsnode script.ts, I got
SyntaxError: await is only valid in async functions and the top level bodies of modules
for the exact line.
Somehow tsnode is creating a normal function instead of an async one. Is there any trick around this?
In my case, the exact fn defintion in string is:
`try{ ${js}\n return await ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
So if I get rid of await, it works ie
`try{ ${js}\n return ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
Still doesn't resolve the main tsnode async-fn creation problem, but this is a trick that unstucks me

TypeError: R.unless is not a function

I have a function that looks like this:
export const maybeDate = R.unless(R.isNil, R.constructN(1, Date));
Typescript never gives me a hard time about this, but the moment I run it through Jest, I get this message with zero feedback on what is going on wrong:
TypeError: R.unless is not a function
It stops giving me problems the moment I change it to the following:
export const maybeDate = () => R.unless(R.isNil, R.constructN(1, Date));
However my app itself stops working.
Back in ramda's github, I was suggested to do
export const maybeDate = (v) => R.unless(R.isNil, R.constructN(1, Date))(v);
But this feels more of a workaround (as much as my crappy non-working solution was) than a solution.
What's the best way to fix this?
Currently all I have installed is this:
"ramda": "^0.27.1"
As you can see, Ramda 0.27.1 has the unless method.
const isEven = (n) => n % 2 === 0;
const shoutOdds = R.unless(isEven, console.log.bind(null, 'this "%s" looks odd'));
shoutOdds(5);
shoutOdds(10); // not called
shoutOdds(7);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
if you use es modules,
please make sure to import the ramda's namespace this way: import * as R from 'ramda'

module.exports = ({}) vs {}

I work with Koa middleware and in some code I use this code in router:
module.exports = ({router}) => {
//some code
}
if I do:
module.exports = {router} => {
//some code
}
the nodejs app throws an error. So whats the difference between these two different exports except the error part?
{(router)} is just wrong. You should see module.exports as a function so it has () where the arguments go, which in this case is an object {} of the functions you want to export (here router, but it could be multiple just as well.
I think this article gives a very clear explanation: https://www.sitepoint.com/understanding-module-exports-exports-node-js/

module.exports is not returning functions in nodejs

Everyone I'm beginner in NODE.JS i'm using module.exports function inside that I have written hello function in it.I tried to import from other file.but it's not working.Can anyone solve this problem.Thanks in advance...
index.js
module.exports=function(){
hello:(data)=>{
return "Good night" +data;
}
}
trail.js
const index=require('./index');
const e=new index();
console.log(e.hello("abc"));
When you're using a function as a constructor, creating new objects from it, you'll have to reference each of those objects with this to assign their properties:
module.exports=function(){
this.hello = (data) => {
return "Good night" +data;
};
}
The syntax <identifier>: has different meanings depending on its placement/surroundings. When used within a function body, at the start of a statement, it only defines a label. To have it define a property, it would have to be used within an object initializer.
I found the solution slightly we have to change the code in index.js.In ES6 Functional constructors are available.we should use this keyword this keywords refers current class objects.because In javascript Functions are first class Objects.correct me If I'm wrong. post the answer if anyone knows Others solution are also always welcome....
Index.js
module.exports=function(){
this.hello=(data)=>{
return "Good night" +data;
}
}
Trail.js
const index=require('./index');
const e=new index();
console.log(e.hello("abc"));
you can also use it this way:
module.exports=function(data){
return "Good night" +data;
}
const temp = require('./index');
console.log(temp('demo developer'));
I dont know what is the correct desicion you tring to find out, but may be you would like to go this way:
index.js:
exports.hello = data => {
return 'Good night ' + data;
};
trail.js:
const e = require('./index');
console.log(e.hello('Jhonny'));

Electron app: Reference mainWindow object in another module?

I am building an electron app, where the mainWindow object is created following the quick start: http://electron.atom.io/docs/tutorial/quick-start/.
As per this quick start, it is created asynchronously. The problem that I run into, is that for instance when I want to send messages from main to renderer process, I need to reference the mainWindow object. If this happens to be in a module that I require, then I need a means to make this module know of the mainWindow object.
I could of course prepend it with global., but I know that this is very much advised against. So I wish to do it more elegantly.
I came across this post: Asynchronous nodejs module exports; which appears to offer a solution. Taking the main.js file from the quick start (see above link, it's explicitly shown there), it appears I would add to the createWindow function
if( typeof callback === 'function' ){
callback(mainWindow);
}
and export the main.js module as
module.exports = function(cb){
if(typeof mainWindow !== 'undefined'){
cb(mainWindow);
} else {
callback = cb;
}
}
Then, in a higher-level script, I would require as follows:
let main = require('./main.js');
let lib = require('./lib.js'); // Library where I need a mainWindow reference
main(function(window) {
lib.doSomething(window);
});
where lib.js looks like
module.exports.doSomething = function(window) {
// Do something with window object, like sending ipc messages to it
window.webContents.send('hello-from-main', "hi!");
}
Although the simple case in the original post 'Asynchronous nodejs module exports' works fine, I cannot get it to work like described above; running the app it complains Uncaught Exception: TypeError: Cannot read property 'webContents' of null. This is also the case if I directly require lib.js within main()'s callback (which I know is also advised against).
I confess that I do not fully understand the simple case of the post, as I am rather new to node. This prevents me from fixing my own implementation of it, which I agree is blunt copy/pasting which reasonably should be expected to fail. Could somebody help me with how to correct above method, or advise me of a different approach to make it work? Thank you!
I have created the npm package electron-main-window for the same.
Install:
$ npm install electron-main-window
or
$ yarn add electron-main-window
Usage:
// Import ES6 way
import { getMainWindow } from 'electron-main-window';
const mainWindow = getMainWindow();
// Import ES5 way
const mainWindow = require('electron-main-window').getMainWindow();
// e.g:
if(mainWindow !== null ){
mainWindow.webContents.send('mainWindowCommunication', "This is a test message");
}
Whooops! The devil is in the details... I had defined on top of main.js
let mainWindow = null, callback;
which caused the error! Should be
let mainWindow, callback;
then it works perfectly!
P.s. Instead of deleting my post, I opted for keeping it and answering myself for future reference of other people who need asynchronous exporting.

Resources