0

I'm new to Typescript and I've problems when I use import/export syntax in the browser. The terminal/compiler doesn't throw any kind of error. But google's console says:

GET http://127.0.0.1:5500/public/modules/file1.mod net::ERR_ABORTED 404 (Not Found)

Simply, I want to import a module inside my main file(main.ts)

index.html

<script type="module" src="./main.js"></script>

tsconfig.json

"module": "ES2015",                          
"rootDir": "./src/",   
"target": "ES2016",                                 
"lib": ["DOM", "ES2019"],  
"esModuleInterop": true,    

this is main.ts and it has to import a simple constant(pigreco)

import { pigreco } from './modules/file1.mod'; 
console.log(pigreco);
7
  • Well do you have a /modules/file1.mod module (and does your setup serve it to the browser)? Commented Feb 8, 2022 at 18:52
  • yes, I want to serve it to the browser. Commented Feb 9, 2022 at 8:36
  • Well the error message says that you aren't doing it. What web server are you using? Where is that file1.mod located? What are its contents? Commented Feb 9, 2022 at 11:32
  • @Bergi The main problem is: Typescript alone can transpile a code that can be read by the browser? ES6 or CommonJS can be understood by Google etc.? Am I forced to use Webpack/Rollup etc.? Commented Feb 9, 2022 at 11:41
  • 1
    ES6 modules are supported by browsers, yes. CommonJS, no. Commented Feb 9, 2022 at 11:43

1 Answer 1

1

just add .ts to the end of your import.
import { pigreco } from './modules/file1.mod.ts';
it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.