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

TS5071 error fires when using mode="esnext", but builds were fine before 3.0.1 #26224

Closed
jpike88 opened this issue Aug 6, 2018 · 9 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@jpike88
Copy link

jpike88 commented Aug 6, 2018

TypeScript Version: 3.0.1

Search Terms:
TS5071

Code

https://github.com/Microsoft/TypeScript/blob/d8cbe34a051a64d39f55f4e46e18ac404aac922e/src/compiler/diagnosticMessages.json#L2887

Expected behavior:
This should work fine with mode="esnext" just like it always did.

Actual behavior:
I've been using the following tsconfig.json file with zero issues before upgrading to 3.0:

{ "compileOnSave": true, "compilerOptions": { "target": "esnext", "module": "esnext", "sourceMap": true, "declaration": true, "declarationDir": "../decl", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "noUnusedLocals": true, "resolveJsonModule": true, "noUnusedParameters": true, "skipLibCheck":true, "outDir":"../dist/", "typeRoots" : ["../node_modules/@types"] }, "exclude":["../node_modules/@types/"] }
Now this error message is firing, but the resolution and import of json files work fine when module is set to esnext. Switching mode to commonjs breaks my import syntax, modules are missing left right and center.

@sheetalkamat, could you help give insight on this?

@jpike88 jpike88 changed the title TS5071 error when using mode="esnext", builds break if using "commonjs" TS5071 error fires when using mode="esnext", but builds were fine before 3.0.1 Aug 6, 2018
@jpike88
Copy link
Author

jpike88 commented Aug 6, 2018

my code originally looked like this and it worked:

import baseConfig from '../../config.json';

The error fired, and if I ignored the error, baseConfig was undefined. so I changed my import syntax to this, and changed mode to "commonjs":

import * as baseConfig from '../../config.json';

baseConfig then was imported correctly.

But it broke import statements for other dependencies, like AWS:

import AWS from 'aws-sdk';

Why was this error introduced to begin with?

@MichalLytek
Copy link

I have the same problem - this error is not listed in breaking changes:
https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes

So I think it should be changed as TS 2.9.1 worked well with resolveJsonModule using esnext and webpack.

@sheetalkamat
Copy link
Member

This is duplicate of #25755

@sheetalkamat
Copy link
Member

--resolveJsonModule is supported only when using nodejs module resolution and emit is commonjs. In other systems json module is not supported natively without any plugins and hence the error. The check for emit was missing earlier rather than intentional allowing it (so your code was always error but we missed on reporting errors on it before)

@sheetalkamat sheetalkamat added the Duplicate An existing issue was already created label Aug 6, 2018
@MichalLytek
Copy link

I get it but I have the same problem as #25755 (comment) - webpack + esnext = tree shaking, so I can't switch to commonjs to satisfy the typescript compiler as webpack handle importing json files very well.

@jpike88
Copy link
Author

jpike88 commented Aug 7, 2018

@sheetalkamat If my code functioned correctly before this error check was implemented, didn't it technically already support it before this error check existed? Theres nothing funky with my code, it's standard syntax (just with allowSyntheticDefaultImports enabled). Why should I be forced to downgrade to commonjs when it was working perfectly fine before?

@jpike88 jpike88 closed this as completed Aug 7, 2018
@wizardnet972
Copy link

@sheetalkamat, any idea how to resolve my json file without the extension? i'm using vscode+typescript.
import jsonData from './file'. but I got this error: [ts] Cannot find module './file'.

resolveJsonModule is true in my tsconfig..

@sheetalkamat
Copy link
Member

You have to explicitly use .json in the module specifier to be able to resolve to a json module. We have this constraint so that the json files don't get included without intentionally choosing to for those specific module specifiers

@wizardnet972
Copy link

I see. @sheetalkamat thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants