Next.js Discord

Discord Forum

Trying To Understand JS vs MJS

Answered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I'm trying to add a redirect while the site is under maintenance, but I've never heard of js before, and I'm not sure how I would translate what the docs are telling me in js -> js.

NextJS Redirect Docs: https://nextjs.org/docs/app/api-reference/next-config-js/redirects#path-matching

NextJS Config Docs: https://nextjs.org/docs/pages/api-reference/next-config-js

It's probably a dumb question, so thanks in advance for your help!
Answered by joulev
Js here means cjs. You can read about cjs vs mjs online, there are quite a few significant differences, but for the case of the config file:

// cjs
module.exports = {
  …
}


// mjs
const config = {…};
export default config;
View full answer

23 Replies

Im very confused. where do you see mjs?
West African LionOP
That's what create-next-app made when it configured the website
Take a screenshot and show me what you mean
West African LionOP
Okay your talking about the config file. Gotcha
West African LionOP
Code:
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
Thats okay, just think of it as regular js
its just special 🙂
West African LionOP
I'm a dumb dumb sorry! I thought I said that but I didn't
My apologies
No worries bud.
You did via the links, I just didnt catch on
Heres a good explanation
Answer
West African LionOP
I'm not sure how to use this example
module.exports = {
  async redirects() {
    return [
      {
        source: '/old-blog/:slug',
        destination: '/news/:slug', // Matched parameters can be used in the destination
        permanent: true,
      },
    ]
  },
}
@joulev Js here means cjs. You can read about cjs vs mjs online, there are quite a few significant differences, but for the case of the config file: js // cjs module.exports = { … } js // mjs const config = {…}; export default config;
West African LionOP
So like this?
/** @type {import('next').NextConfig} */
const nextConfig = {
  async redirects() {
    return [
      {
        source: "/old-blog/:slug",
        destination: "/news/:slug", // Matched parameters can be used in the destination
        permanent: true,
      },
    ];
  },
};

export default nextConfig;
I beleive that looks syntax correct.
West African LionOP
Thank you both!
never head of cjs either
so time to learn lol