node naming ai_generated true

Cannot find module './my-component' — hyphenated folder misresolved or confused with npm scope

ID: node/require-hyphenated-local-folder

Also available as: JSON · Markdown
80%Fix Rate
85%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

require('./my-component') works but is confused with npm scoped packages or case-insensitive filesystems create mismatches.

generic

Workarounds

  1. 90% success Use consistent casing and exact folder name in require/import path
    import MyComponent from './my-component/index.js'  // explicit exact path

    Sources: https://nodejs.org/api/esm.html

  2. 85% success Configure module aliases in tsconfig/webpack to abstract the hyphenated path
    { "compilerOptions": { "paths": { "@components/*": ["./src/my-component/*"] } } }

    Sources: https://www.typescriptlang.org/tsconfig#paths

Dead Ends

Common approaches that don't work:

  1. Change require to use underscore version 90% fail

    If the actual folder has hyphens, changing to underscores creates a mismatch

  2. Add 'main' field in sub-folder package.json 70% fail

    Adding package.json in every sub-folder creates unnecessary complexity for a naming issue