O
O
Onotolius2022-03-18 22:10:17
JavaScript
Onotolius, 2022-03-18 22:10:17

Why is no value returned from module in javascript in nodejs?

Hello.
I have 2 files:
test1.js

import JSBI from './jsbi.js';
console.log( JSBI.BigInt('TEST!') );


And the jsbi.js file

class JSBI extends Array {
  static BigInt(arg) {
    return arg;
  }
}
export default { JSBI, BigInt };


When I call test1.js with this command "node test1.js", then for some reason I get an error SyntaxError: Cannot convert TEST! to a BigInt
I'm not converting anything, I'm just calling the BigInt method of the JSBI class, it should just return "TEST!"
nodejs I have version 14
Who knows what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2022-03-18
@Login8

When you export the { JSBI, BigInt } object as default in the jsbi.js file, you essentially exported the object outside:
{
JSBI: JSBI,
BigInt: BigInt
}
where BigInt is a standard JavaScript function that converts the passed argument to the BigInt type, then you import this whole object in the test1.js file, in fact the whole object
{
JSBI: JSBI,
BigInt: BigInt
}
is now stored in a JSBI variable, and when you call JSBI.BigInt, you call a non-static class method (to call it you need was to write JSBI.JSBI.BigInt() ), and you call the BigInt method which converts the argument to a type, which is why it swears, since the string cannot be converted to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question