gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.14k stars 10.33k forks source link

Invoking imported function inside `createPages` results in error #38904

Closed marijoo closed 3 months ago

marijoo commented 3 months ago

Preliminary Checks

Description

I have an utility function I outsourced into a singular file. I narrowed the functionality down to just returning the input string just to make sure it’s no problem in the function itself:

// utils/slug.ts
export const slug = (slug: string) => string;

If I import and call this method during the createPages() hook the build will break, no matter what I do. Placing the function directly into gatsby-node.ts works just fine.

import type { GatsbyNode } from "gatsby";
import { slug } from "@utils/slug";

export const createPages: GatsbyNode["createPages"] = async (...props) => {
  ...
  slug("test"); // <-- breaks
}

Reproduction Link

//

Steps to Reproduce

Expected Result

I’d expect to be able to import a function from a file.

Actual Result

The build breaks. The error is always something like this:

 ERROR #11321  API.NODE.EXECUTION

"gatsby-node.js" threw an error while running the createPages lifecycle:

Cannot access 'l' before initialization

Environment

System:
    OS: macOS 14.4
    CPU: (10) arm64 Apple M1 Max
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.volta/tools/image/node/18.19.1/bin/node
    Yarn: 1.22.18 - ~/.volta/tools/image/yarn/1.22.18/bin/yarn
    npm: 10.2.4 - ~/.volta/tools/image/node/18.19.1/bin/npm
  Browsers:
    Chrome: 122.0.6261.129
    Safari: 17.4
  npmPackages:
    gatsby: ^5.13.3 => 5.13.3
    gatsby-plugin-i18n-l10n: ^5.6.1 => 5.6.1
    gatsby-plugin-image: ^3.13.1 => 3.13.1
    gatsby-plugin-layout: ^4.13.1 => 4.13.1
    gatsby-plugin-manifest: ^5.13.1 => 5.13.1
    gatsby-plugin-postcss: ^6.13.1 => 6.13.1
    gatsby-plugin-react-svg: ^3.3.0 => 3.3.0
    gatsby-plugin-sharp: ^5.13.1 => 5.13.1
    gatsby-plugin-sitemap: ^6.13.1 => 6.13.1
    gatsby-source-filesystem: ^5.13.1 => 5.13.1
    gatsby-source-sanity: ^7.9.1 => 7.9.1
    gatsby-transformer-sharp: ^5.13.1 => 5.13.1

Config Flags

No response

marijoo commented 3 months ago

I just found out the problem was webpack import aliases inside the import, so this wouldn’t work:

import { slug } from "@utils/slug";

while this does:

import { slug } from "../src/utils/slug";

Sorry for disturbing.