Skip to content

Using recommended file extension to import TypeScript file causes error "Module not found" #33056

@jameshfisher

Description

@jameshfisher

Run next info (available from version 12.0.8 and up)

$ npx --no-install next --version
Next.js v12.0.7

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

16.13.1

What browser are you using?

n/a

What operating system are you using?

Windows

How are you deploying your application?

n/a

Describe the Bug

When importing a TypeScript file using a .js file extension, Next fails with "Module not found"

$ npm run build

> build
> next build

info  - Checking validity of types...
info  - Creating an optimized production build...
Failed to compile.

./pages/index.tsx
Module not found: Can't resolve '../lib/helper.js' in 'C:\Users\james\dev\foobar\pages'


> Build failed because of webpack errors

Expected Behavior

Next.js should resolve TypeScript imports. TypeScript recommends using the .js file extension to import TypeScript files:

My Next.js application is not an isolated project; it shares TypeScript modules with other environments that rely on this .js file extension.

To Reproduce

  1. echo 'export const n = 5' > lib/helper.ts
  2. In pages/index.tsx, add: import {n} from '../lib/helper.js'
  3. Run next build or next dev

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions