最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

webpack - next.js figlet Error: ENOENT: no such file or directory, open 'pathToProject.nextserverfontsStandard.flf&#

programmeradmin0浏览0评论

I created a new next.js app using npx create-next-app@latest (next.js 15 at the current time)

Then I removed the node_module folder and run

pnpm install

It was successful and with

pnpm run dev

I saw the default home page of create-next-app

Then I install figlet using pnpm and use it in my home page file.

import figlet from "figlet";

export default function Home() {
    const homeFigletText = figlet.textSync(`Home!`);

At this point I get this error in home page:

Error: ENOENT: no such file or directory, open 'pathToProject\.next\server\fonts\Standard.flf'

src\app\page.tsx (6:25) @ Home

  4 |
  5 | export default function Home() {
> 6 |   const homeFigletText = figlet.textSync(`Home!`);

During searches, I found out that Next.js will automatically trace each page and its dependencies to determine all of the files that are needed for deploying. It statically analyze import, require, and fs usage to determine all files that a page might load

But it somehow failed to trace figlet fonts

And figlet Getting Started - Webpack / React recommend to import fonts using importable-fonts (and this workaround actually works and mandatory for client components)

// @ts-ignore
import standard from "figlet/importable-fonts/Standard";
figlet.parseFont("Standard", standard);

But I used to use figlet without importable-fonts Is there any way to tell next.js that copy figlet fonts to .next/server folder?

I created a new next.js app using npx create-next-app@latest (next.js 15 at the current time)

Then I removed the node_module folder and run

pnpm install

It was successful and with

pnpm run dev

I saw the default home page of create-next-app

Then I install figlet using pnpm and use it in my home page file.

import figlet from "figlet";

export default function Home() {
    const homeFigletText = figlet.textSync(`Home!`);

At this point I get this error in home page:

Error: ENOENT: no such file or directory, open 'pathToProject\.next\server\fonts\Standard.flf'

src\app\page.tsx (6:25) @ Home

  4 |
  5 | export default function Home() {
> 6 |   const homeFigletText = figlet.textSync(`Home!`);

During searches, I found out that Next.js will automatically trace each page and its dependencies to determine all of the files that are needed for deploying. It statically analyze import, require, and fs usage to determine all files that a page might load https://nextjs./docs/pages/api-reference/config/next-config-js/output

But it somehow failed to trace figlet fonts

And figlet Getting Started - Webpack / React recommend to import fonts using importable-fonts (and this workaround actually works and mandatory for client components)

// @ts-ignore
import standard from "figlet/importable-fonts/Standard";
figlet.parseFont("Standard", standard);

But I used to use figlet without importable-fonts Is there any way to tell next.js that copy figlet fonts to .next/server folder?

Share Improve this question asked Mar 4 at 2:41 MahpooyaMahpooya 5591 gold badge6 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

One workaround I made is by adding a custom plugin to webpack, that copy those files

First I add cpr for having cross-platform cp -r cli command

pnpm add --dev cpr

Then add copyFigletFonts to your package.json scripts

    "scripts": {
        "copyFigletFonts": "cpr ./node_modules/figlet/fonts/ ./.next/server/fonts/ -o",
    },

Then in your next.config.ts add this:

import { Compiler } from 'webpack';
const exec = require('child_process').exec;
import * as fs from "node:fs";

const nextConfig: NextConfig = {
    ...
    webpack: (
        config,
        {buildId, dev, isServer, defaultLoaders, nextRuntime, webpack}
    ) => {
        // console.log(config);
        config.plugins.push(
            {
                apply: (compiler: Compiler) => {
                    compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
                        if (!fs.existsSync("./.next/server/fonts/")) {
                            console.log('Copy figlet fonts to .next/server/fonts...');
                            exec('npm run copyFigletFonts', (error: string, stdout: string, stderr: string) => {
                                // if (stdout) process.stdout.write(stdout);
                                if (stderr) process.stderr.write(stderr);
                            });
                        }
                    });
                }
            }
        )

        // Important: return the modified config
        return config
    },

    ...
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论