If you want to import a ts
or es
file inside a js
file to execute it using node, you need to be able to transpile the imported files to javascript on fly. You can use babel-register
plugin to achieve this.
You will have to include babel-register in the index.js like this
require('babel-register')({
plugins: [/* List of plugins */],
cache: process.env.NODE_ENV !== 'development'
});
If you use babel.rc, you dont need to include the plugins here. You can use @babel/preset-typescript
plugin to transpile ts
to js
.
Edit:
You can import any exported values from a ts file, just like you would import from any other file.
var Hello = require('file1.js')