Fixing “Cannot mix BigInt” Error in JavaScript
If you see TypeError: Cannot mix BigInt and other types, use explicit conversions in JavaScript, your expression mixes a BigInt (an integer with a trailing n, such as 10n) with a regular number (Number) in the same calculation. To fix it, make both operands of + or / the same type, either BigInt or Number. The error goes away.
It appears when you measure elapsed time in nanoseconds with Node.js's process.hrtime.bigint() and convert it to milliseconds, or when you add up BigInt values starting from 0. I triggered this error in a test environment, and I confirmed that it disappears after the fix.
The test environment was Ubuntu 24.04, mainly with Node.js 24.22.3. For comparison, I also ran the code with Node.js 22.23.5. For the browser, I used Chromium 158 driven by Playwright, and I only checked that the message is the same. I did not test Firefox or Safari, the errors that TypeScript's type checker reports, or the error from converting BigInt to a string with JSON.stringify. In the output, I replaced the test environment's working folder with /path/to/app.
The Full Error
I prepared the following script (elapsed.mjs), which prints the time a process takes in milliseconds.
// Print the elapsed time in milliseconds
const start = process.hrtime.bigint();
await new Promise((resolve) => setTimeout(resolve, 50));
const end = process.hrtime.bigint();
const ms = (end - start) / 1e6;
console.log(`${ms} ms`);
When I ran it with Node.js 24.22.3, it stopped with the following output (exit code 1).
$ node elapsed.mjs
file:///path/to/app/elapsed.mjs:6
const ms = (end - start) / 1e6;
^
TypeError: Cannot mix BigInt and other types, use explicit conversions
at file:///path/to/app/elapsed.mjs:6:26
Node.js v24.22.3
With Node.js 22.23.5, the only difference was the version in the last line. The ^ position and :6:26 point to the / on line 6.
As described in the Node.js documentation for process.hrtime.bigint(), this function returns a nanosecond timestamp as a BigInt. end - start subtracts one BigInt from another, so that part is fine. However, 1e6 is a Number. The error occurred when the code tried to divide a BigInt by a Number.
Which Expressions Cause the Error
I ran each expression that mixes BigInt and Number and printed the result (Node.js 24.22.3; Node.js 22.23.5 gave the same results).
1n + 1 => TypeError: Cannot mix BigInt and other types, use explicit conversions
1n - 1 => TypeError: Cannot mix BigInt and other types, use explicit conversions
2n * 3 => TypeError: Cannot mix BigInt and other types, use explicit conversions
10n / 2 => TypeError: Cannot mix BigInt and other types, use explicit conversions
10n % 3 => TypeError: Cannot mix BigInt and other types, use explicit conversions
2n ** 2 => TypeError: Cannot mix BigInt and other types, use explicit conversions
1n + 1.5 => TypeError: Cannot mix BigInt and other types, use explicit conversions
1n & 1 => TypeError: Cannot mix BigInt and other types, use explicit conversions
1n << 1 => TypeError: Cannot mix BigInt and other types, use explicit conversions
1n >>> 0n => TypeError: BigInts have no unsigned right shift, use >> instead
+1n => TypeError: Cannot convert a BigInt value to a number
Math.max(1n, 2) => TypeError: Cannot convert a BigInt value to a number
Math.round(10n) => TypeError: Cannot convert a BigInt value to a number
1n < 2 => true
2n > 1.5 => true
1n == 1 => true
1n === 1 => false
"id:" + 1n => "id:1"
`${1n}` => "1"
1n + "1" => "11"
Arithmetic such as addition and division produced the same "Cannot mix BigInt" error whenever one operand was a Number. MDN's BigInt explanation also says that BigInt cannot be mixed with Number in operations and that both operands must be the same type.
When I tried to convert with a leading +, or passed a BigInt to Math.max or Math.round, the message changed to "Cannot convert a BigInt value to a number." As the same explanation notes, Math functions do not accept BigInt. As the operator section on the same page explains, the unsigned right shift operator >>> cannot be used with BigInt, even when both operands are BigInt.
On the other hand, comparisons with < and == did not throw an error, even when the types were mixed. However, 1n === 1 is false. Joining with + to a string also did not throw an error and produced a string.
Chromium behaves the same way. On a page that ran const total = 10n; console.log(total + 1);, I got the Cannot mix BigInt and other types, use explicit conversions error. Firefox and Safari show different messages. MDN's error reference page says that Firefox shows can't convert BigInt to number and Safari shows something like Invalid mix of BigInt and other type in addition/multiplication/…..
Fix 1: Match Both Sides to BigInt
If an integer result is enough, convert the Number side to BigInt. Adding n after a number makes it a BigInt value.
// Print the elapsed time in milliseconds (divide while still BigInt)
const start = process.hrtime.bigint();
await new Promise((resolve) => setTimeout(resolve, 50));
const end = process.hrtime.bigint();
const ms = (end - start) / 1_000_000n;
console.log(`${ms} ms`, typeof ms);
$ node elapsed-bigint.mjs
50 ms bigint
The error went away, and the result was the BigInt 50. Node.js 22.23.5 gave the same result.
BigInt division truncates the fractional part. 10n / 3n gives 3n, and -10n / 3n gives -3n. Unlike Number's 10 / 3, it does not give 3.3333333333333335. If you need fractional milliseconds, use Fix 2 below.
To convert a Number held in a variable to BigInt, use BigInt(x). However, you cannot convert a fractional number.
BigInt(3) => 3n
BigInt(1.5) => RangeError: The number 1.5 cannot be converted to a BigInt because it is not an integer
BigInt(Math.trunc(1.5)) => 1n
BigInt("42") => 42n
BigInt("1.5") => SyntaxError: Cannot convert 1.5 to a BigInt
new BigInt(1) => TypeError: BigInt is not a constructor
If the value might be fractional, convert it to an integer first with Math.trunc or a similar function, then pass it to BigInt(). MDN's BigInt() explanation also notes that calling it with new throws an error.
Fix 2: Convert to Number and Then Calculate
When you want a fractional result or need to use Math functions, convert the BigInt side to a Number with Number() before calculating.
// Print the elapsed time in milliseconds (convert to Number, then divide)
const start = process.hrtime.bigint();
await new Promise((resolve) => setTimeout(resolve, 50));
const end = process.hrtime.bigint();
const ms = Number(end - start) / 1e6;
console.log(`${ms} ms`, typeof ms);
$ node elapsed-number.mjs
50.369003 ms number
The error went away, and the result was a Number that includes fractional milliseconds. Since this is a measured time, the value changes a little on each run.
When converting with Number(), watch out for values that are too large. As MDN's explanation of Number.MAX_SAFE_INTEGER states, Number can represent integers exactly only up to 9007199254740991. When I passed 2n ** 53n + 1n (9007199254740993) to Number(), I got 9007199254740992, which is one less. That is the same value as passing 2n ** 53n. No error is thrown, so you cannot notice the value has changed.
If a value is a BigInt because it must be handled exactly, such as a database ID, converting it with Number() can change the lower digits. It may even become the same value as a different ID, so keep those values as BigInt when you calculate with them.
If you don't know how large the value is, check its range before converting. That prevents calculations from continuing with a shifted value.
// Convert a BigInt to a Number only when it can be represented exactly
function toSafeNumber(big) {
if (big > BigInt(Number.MAX_SAFE_INTEGER) || big < BigInt(Number.MIN_SAFE_INTEGER)) {
throw new RangeError(`${big} は Number では正確に表せません`);
}
return Number(big);
}
console.log(toSafeNumber(123n) + 1);
try {
console.log(toSafeNumber(2n ** 53n + 1n) + 1);
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
$ node to-safe-number.mjs
124
RangeError: 9007199254740993 は Number では正確に表せません
Set the Initial Value of the Sum to 0n
When you sum BigInt values, writing the initial value as 0 makes it a Number, so adding a BigInt to it throws an error.
// Sum an array of BigInts (the initial value is the Number 0)
const sizes = [1024n, 2048n, 4096n];
let total = 0;
for (const size of sizes) {
total += size;
}
console.log(total);
$ node sum.mjs
file:///path/to/app/sum.mjs:5
total += size;
^
TypeError: Cannot mix BigInt and other types, use explicit conversions
at file:///path/to/app/sum.mjs:5:12
After this, the output continues with three lines pointing into Node.js internals and the Node.js v24.22.3 line, which I have omitted here. The error occurs at the first step, 0 + 1024n. When I changed the initial value to 0n, the error went away and the output was 7168n. With reduce, passing 0n as the second argument, as in sizes.reduce((a, b) => a + b, 0n), also gave 7168n.





