Next.js Discord

Discord Forum

Can't import inside `next.config.ts`

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I'm using t3 env, and trying to import the env config object and use it inside the next.config.ts, but it fails with this error when I start the dev server or build:
 ⨯ Failed to load next.config.ts, see more info here https://nextjs.org/docs/messages/next-config-error
SyntaxError: Cannot use 'import.meta' outside a module
    at ignore-listed frames


Just importing it doesn't cause the error, but when i use it, then it does. This is my next.config.ts:
import path from 'path';
import type { NextConfig } from 'next';

import { envConfig } from './config/env'; // <------ just importing is okay
import packageJson from './package.json';

const nextConfig: NextConfig = {
  transpilePackages: ['@repo/common'],

  // Expose Vercel environment to the client
  env: {
    VERCEL_ENV: envConfig.VERCEL_ENV, // <------ when I use it, it throws the error
    NEXT_PUBLIC_VERSION: packageJson.version,
  },

  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'maps.googleapis.com',
      },
    ],
  },

  // disable all browser cache
  async headers() {
    return [
      {
        source: '/(.*)',
        headers: [
          {
            key: 'Cache-Control',
            value: 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
          },
        ],
      },
    ];
  },

  sassOptions: {
    includePaths: [path.join(__dirname, 'styles')],
  },

  turbopack: {
    resolveAlias: {
      'fs/promises': {
        browser: './empty.ts',
      },
    },
  },
};

export default nextConfig;

6 Replies

Giant pandaOP
bump
Longtail tuna
show package.json
seems like including "type": "module" should fix ur problem
@Longtail tuna seems like including "type": "module" should fix ur problem
Giant pandaOP
it has "type": "module", no difference
Giant pandaOP
bump