Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jameshfisher opened this issue Jan 6, 2022 · 4 comments
Labels
bug Issue was opened via the bug report template.

Comments

@jameshfisher
Copy link
Contributor

jameshfisher commented Jan 6, 2022

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
@jameshfisher jameshfisher added the bug Issue was opened via the bug report template. label Jan 6, 2022
@sigma54-ca
Copy link

I'm having the same issue... have you found any temporary workarounds?

@jameshfisher
Copy link
Contributor Author

No workarounds - other than changing the import to .ts or removing the extension. Which then breaks other things that expect the .js extension. Not a good situation.

@timneutkens
Copy link
Member

Duplicate of #32237

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants