Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

778
Visualizações
NextJs Next.config.mjs import local constant file

Importing local files in next.config

The issue

Hi, The issue we are facing is wanting to create a dynamic next config. This works fine when we do not import any files but when trying to import a constant file in the next.config.mjs, things break.

We are on next version 12.1 in order to use the MJS variant of the config file.

The constant file is a simple JS file with some values to determine the locales that we need to use for the different brands we support. These are used on multiple places, that is why we do not wan't to hardcode these values in here too.

import { locales } from './src/constants/locales'; // It breaks on this import

const currentPlatform = process.env.NEXT_PUBLIC_PLATFORM;

module.exports = () => ({
  i18n: {
    localeDetection: false,
    ...locales[currentPlatform],
  },
  images: {
    domains: [
      // Our domains
    ],
  },
  async rewrites() {
    return [
      // Our rewrites
    ];
  },
  webpack: (config, { defaultLoaders }) => {
    config.module.rules.push(
      // Our config
    );

    return config;
  },
});

The error message

This is the error that is thrown when we try to start the dev server, suggesting the import path is wrong. (it is not.)

error - Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/*****/Development/****/********/src/constants/locales' imported from /Users/******/Development/******/******/next.config.mjs
Did you mean to import ../src/constants/locales.js?
    at finalizeResolution (internal/modules/esm/resolve.js:276:11)
    at moduleResolve (internal/modules/esm/resolve.js:699:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:86:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
  • = Censored pathnames

Any tips, tricks or extra info is welcome!

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Perhaps you are using .vercelignore and not include the file/folder. I made the same mistake, in my case I not included !/lingui.config.js.

> Build error occurred
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/vercel/path0/lingui.config.js' imported from /vercel/path0/next.config.mjs
    at new NodeError (internal/errors.js:322:7)
    at moduleResolve (internal/modules/esm/resolve.js:731:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:842:11)
    at Loader.resolve (internal/modules/esm/loader.js:89:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:76:40)
    at link (internal/modules/esm/module_job.js:75:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

To fix, I add the missing file:

/*

!/public
!/src
!/lingui.config.js # <-- my mistake
!/next-env.d.ts
!/next.config.mjs
!/package.json
!/postcss.config.js
!/tailwind.config.cjs
!/tsconfig.json
!/yarn.lock
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda