I am trying to import a typescript file (src/index.ts) in a javascript file(tests/steps/utils.js)
But when I use const index_1 = require("../../src/index"); in my javascript file, it gives an error: Cannot find module '../../src/index'
But the file is right there. Is there any other way to import a typescript file into a javascript file?
You can't. You need to compile the typescript files into js first. However, you can import the built typescript file i.e. js file from its build directory.
Updated May 2023: Just found this library: ts-import, it seems can import TS into JS project already.
You can't import a ts into js file.
If you really need the module in .ts file, try to import from dist folder, where the ts compiler output path, or refactor into javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With