Typescript compiler is forgetting to add file extensions to ES6 module imports?

后端 未结 2 1101
遇见更好的自我
遇见更好的自我 2021-01-01 16:09

I am trying to compile a Typescript project to JS with ES6 module resolution, but something seems not right.

My tsconfig.json looks like this:



        
相关标签:
2条回答
  • 2021-01-01 16:44

    Keith answered it correctly.

    In your main.ts

    instead of

    import { testText } from 'module1';
    

    try the following

    import { testText } from 'module1.js';
    

    In my case vscode intellisense works + I get the desired output file main.js as well.

    0 讨论(0)
  • 2021-01-01 16:47

    This is a bug in TypeScript.

    In the short term you can work around it by specifying the output file:

    in main.ts specify the .js extension and path:

    import { testText } from './module1.js';
    alert(testText);
    

    This will pick up module.ts correctly, but output with the .js extension included.

    Note that you also need to prefix local files with ./ as 'bare' module names are reserved for future use.

    0 讨论(0)
提交回复
热议问题