Categories
JavaScript Answers

How to fix referenceerror: localstorage is not defined with JavaScript?

To fix referenceerror: localstorage is not defined with JavaScript, we should make sure we spell it correctly and only try to use it in the browser environment.

For instance, we write

const foo = localStorage.getItem("foo");

to call localStorage.getItem with 'foo' to return the value of the local storage entry with key 'foo'.

And we write

localStorage.setItem("foo", 1);

to call setItem to set the item with key 'foo' to value 1.

And we write

localStorage.removeItem("foo");

to call removeItem to remove the item with key 'key'.

We should only try to use these methods in code that runs in the browser.

Categories
JavaScript Answers

How to convert HTML to PDF with Node.js and JavaScript?

To convert HTML to PDF with Node.js and JavaScript, we use the html-pdf-node package.

To install it, we run

npm i html-pdf-node

Then we write

const htmlToPdf = require("html-pdf-node");

const options = { format: "A4" };
const file = { content: "<h1>Welcome to html-pdf-node</h1>" };
const pdfBuffer = await htmlToPdf.generatePdf(file, options);
console.log("PDF Buffer:-", pdfBuffer);

to convert the file.content string into a buffer with the PDF file with the generatePdf function.

We can also write

const htmlToPdf = require("html-pdf-node");

const options = { format: "A4" };
const file = { url: "https://example.com" };
const pdfBuffer = await htmlToPdf.generatePdf(file, options);
console.log("PDF Buffer:-", pdfBuffer);

to call generatePdf to get the PDF from the file.url URL and convert that to a PDF buffer.

A promise is returned so we use await to get the pdfBuffer.

Categories
JavaScript Answers

How to fix referenceerror: exports is not defined in ES module scope with JavaScript?

To fix referenceerror: exports is not defined in ES module scope with JavaScript, we should make sure we’re using export in an ES module instead of module.exports.

For instance, we write

const foo = () => {};

export { foo };

to create an ES module that exports the foo function as a named export.

We can also create a default export with

const foo = () => {};

export default foo;

to export foo as a default export.

module.exports is only used in CommonJS modules to export their members.

Categories
JavaScript Answers

How to mock a date in Jest and JavaScript?

To set a mock date in Jest and JavaScript, we can use the useFakeTimers and setSystemTime methods.

For instance, we write:

jest
  .useFakeTimers()
  .setSystemTime(new Date('2022-01-01').getTime());

to call useFakeTimers to let us mock timers.

Then we call setSystemTime to set the time of the mock timer by calling it with a timestamp in milliseconds.

Categories
JavaScript Answers

How to fix cannot read properties of undefined (reading ‘push’) error with JavaScript?

To fix cannot read properties of undefined (reading ‘push’) error with JavaScript, we should make sure we’re calling push on an array.

For instance, we write

const arr = [1, 2, 3, 4];
arr.push(5);

to call push on arr with 5 to append 5 as the last item of the arr array.

We can check if a variable is an array with the Array.isArray method.