I have a index.ts and a User.ts file
the index.ts file imports User:
import User from './User'
I then use tsc to transpile the ts files to js files and then I run the index file:
tsc index.ts node index.js
Unfortunately this gives me this error:
(node:1672) UnhandledPromiseRejectionWarning: C:\temp\dev\models\User.ts:1
(function (exports, require, module, __filename, __dirname) { import {Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn} from "typeorm"
^^^^^^
SyntaxError: Unexpected token import
So it seems that my import statement import User from './User' is transpiled to User = require('./User') and still imports the User.ts file instead of the User.js
The TypeScript compiler won't automatically compile things that you import - if that's something you want, you're best off using something like Webpack or Parcel. To get all of the files in your project to compile and import properly, you'll have to explicitly ask
tscto do so.There's two ways you can do this:
tsc- e.g.tsc index.ts User.ts..tsconfigfile in the directory, you can just runtscto compile all.tsfiles. Note that if you have afileslist in your.tsconfig, it'll just compile those files.