Next.js Discord

Discord Forum

How do I get Next.JS to ignore certain files for linting during the build phase

Unanswered
Chartreux posted this in #help-forum
Open in Discord
ChartreuxOP
When I run 'next build', it doesn't exclude files such as src/components/abc.test.tsx

It does exclude the __tests__ folder.

My tsconig exclude is:

"exclude": ["node_modules"]

I did try modifying it to:

"exclude": ["node_modules", "**/*.test.tsx"]

but no joy. It still compiles a .test.tsx file in the src/components folder.

Can anyone please provide some guidance on this? I didn't find documentation regarding the default excludes behaviour in Next.

6 Replies

Next only builds code that you actually use inside your pages and route handlers. If a test file is getting compiled it's likely because you imported it somewhere where you shouldn't have.
ChartreuxOP
hi, i've verified that this is not the case and that it's not being imported anywhere. I did this by checking git grep 'abc.test' and it yields nothing.
ChartreuxOP
bump
Can you post the build logs? Censor any sensitive information if you need to.

From your initial request it's not clear if you're getting errors at build time or not.
ChartreuxOP
i did some more digging and noticed that it's actually failing at the lint stage:

./node_modules/.bin/next build                                   INT ✘  02:35:30 PM
info  - Loaded env from /Users/vina/software/unibloom/primary/.env.local

Failed to compile.

./src/modules/analytics/components/EmissionWaterfall/EmissionWaterfall.test.tsx
264:9  Error: 'a' is assigned a value but never used. Allowed unused vars must match /^_/u.  @typescript-eslint/no-unused-vars
264:9  Error: 'a' is never reassigned. Use 'const' instead.  prefer-const

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
info  - Linting and checking validity of types .%


And then i found [these docs](https://nextjs.org/docs/app/building-your-application/configuring/eslint#linting-custom-directories-and-files).

There's the dirs option, but i only want to ignore .*test.* files during next's linting phase.
That's much easier to solve. You needed to look up ESLint's docs.

You can ignore files with a .eslintignore file. :cat_coding:

Here: https://eslint.org/docs/latest/use/configure/ignore