question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when accessing Typescript enum defined outside Next root in Next 9.4

See original GitHub issue

Bug report

Accessing a typescript enum property when the file defining the enum is outside Next’s directory causes issues in Next 9.4.0 and Next 9.4.1, but not in Next 9.3.6.

Describe the bug

Defining a Typescript enum in a .ts file outside the root causes issues running next if any property is accessed on the enum.

The error implies that the file was not parsed correctly:

../lib/common/types.ts 1:7
Module parse failed: Unexpected token (1:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> export enum MyEnum {
|   OPTION_ONE = 1,
|   OPTION_TWO = 2,

This was not a problem in Next 9.3.6.

It’s hard to describe, so I put together a minimal reproduction: https://github.com/majelbstoat/nextjs-broken-enums

Expected behavior

Typescript is compiled correctly, and I can access properties on enums.

System information

  • OS: [macOS]
  • Version of Next.js: [9.4.0, 9.4.1]
  • Version of Node.js: [12.16.1]

Additional context

This broke in 9.4.0, and I originally thought it was the definition itself that caused the problem because that’s what the error implied (https://github.com/zeit/next.js/issues/12786) but that was a symptom, not the root issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:23 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
flolucommented, Jul 30, 2021

It seems as if this experimental configuration in next.config.js fixes the problem:

module.exports = {
  experimental: {
    externalDir: true,
  },
}
4reactions
benwindingcommented, Dec 1, 2020

Here’s what worked for me, (based on @majelbstoat), using the function version of next.config.js

// This uses phases as outlined here: https://nextjs.org/docs/api-reference/next.config.js/introduction
module.exports = (phase, { defaultConfig }) => {
  return {
    ...defaultConfig, // This is important
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
      fixEnums(config); // This is important
      return config
    },
  }
}

function fixEnums(config) {
  config.module.rules.forEach(({ use }, i) => {
    if (!use) return
    const isBabelLoader = Array.isArray(use)
      ? use.findIndex((item) => item && item.loader && item.loader === 'next-babel-loader') !== -1
      : use.loader === 'next-babel-loader'
    if (isBabelLoader) {
      delete config.module.rules[i].include
    }
  })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why my enum type wont work in Next.js project - Stack Overflow
One solution could be that you define the enum type first as shown below, export enum ISide { LEFT = 'left', RIGHT =...
Read more >
Google JavaScript Style Guide
Classes, enums, functions, constants, and other symbols are exported using the exports object. Exported symbols may be defined directly on the exports object, ......
Read more >
Firebase JavaScript SDK Release Notes - Google
The earlier update in version 9.13.0 upgraded TypeScript only in the root. Fixed a bug that caused Firebase SDKs to throw an error...
Read more >
Handbook - Enums - TypeScript
An enum can be defined using the enum keyword. ... It is a compile time error for constant enum expressions to be evaluated...
Read more >
ts-loader | Yarn - Package Manager
The simple solution is to disable it by using the transpileOnly: true option, but doing so leaves you without type checking and will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found