I'm running Node v14.5.0. I'm using ts-node-dev
in dev enviroment
Trying to compile to JS always gives an error.
At first, I tried with this tsconfig
:
"target": "es5",
"module": "commonjs"
"outDir": "./dist",
"rootDir": "./src"
But when I run tsc
and node dist/app.js
I get the following error:
UnhandledPromiseRejectionWarning: D:\Dev\src\entity\BaseEntity.ts:1
import {
^^^^^^
SyntaxError: Cannot use import statement outside a module
After doing some research, I found that I should tell Node to use Modules adding "type": "module"
to package.json
. After that, the error changed to:
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
Doing some more research I tried several different things like:
Changing tsconfig
from "module": "commonjs"
to "module": "es2015"
This also causes to be unable to run ts-node-dev
. It throws the error: Cannot use import statement outside a module
.
Adding to tsconfig
"moduleResolution": "node"
With this and "es2015" module tsconfig I'm requered in all my typescript imports to use the .JS extension. Example: import { foo } from '../bar.js
, otherwise throws an error like: Error [ERR_MODULE_NOT_FOUND]: Cannot find module D:/...
. And of course, prevents me from using ts-node-dev
as throws an error described above.
The problem was in the ormconfig.js
where my entities path was pointing to a .ts
file instead of a .js
.
Solution:
entities: ['dist/entity/*.js'],
migrations: ['dist/migration/**/*.js']