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
TypeScript Answers

How to fix error TS2339: Property ‘x’ does not exist on type ‘Y’ with TypeScript?

To fix error TS2339: Property ‘x’ does not exist on type ‘Y’ with TypeScript, we need to define the property in the interface with the right type.

For instance, we write

interface Images {
  main: string;
  [key: string]: string;
}

const getMain = (images: Images): string => {
  return images.main;
};

to define the Images interface with the main property.

Then we can get the value of the images.main property without an error since the Images interface has the main property and it’s a string.

If the property name and its type matches, then the error should go away.

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.

Categories
JavaScript Answers

How to fix hostname/IP does not match certificate’s altnames with Node.js and JavaScript?

To fix hostname/IP does not match certificate’s altnames with Node.js and JavaScript, we create a proxy.

For instance, we write

const httpProxy = require("http-proxy");
const proxy = httpProxy.createProxyServer();

proxy.web(req, res, {
  changeOrigin: true,
  target: "https://example.com:3000",
});

to create a proxy with the http-proxy package.

We call createProxyServer to create the proxy.

And we set the target to the destination URL to proxy to.

If we’re using HTTPS, we write

const httpProxy = require("http-proxy");

httpProxy
  .createServer({
    ssl: {
      key: fs.readFileSync("valid-ssl-key.pem", "utf8"),
      cert: fs.readFileSync("valid-ssl-cert.pem", "utf8"),
    },
    target: "https://example.com:3000",
    secure: true,
  })
  .listen(443);

to call createServer to create the proxy server.

We set the SSL key and certificate files.

And we set the target to the destination URL to proxy to.

We set secure to true to let us proxy HTTPS traffic.