I currently have Node v14.5.0 installed and I'm using ts-node-dev
in my development environment
However, I am encountering an error every time I try to compile to JS.
Initially, I attempted with the following tsconfig
:
"target": "es5",
"module": "commonjs"
"outDir": "./dist",
"rootDir": "./src"
When running tsc
and node dist/app.js
, I receive the following error:
UnhandledPromiseRejectionWarning: D:\Dev\src\entity\BaseEntity.ts:1
import {
^^^^^^
SyntaxError: Cannot use import statement outside a module
Upon further investigation, I discovered that I needed to specify that Node should use Modules by adding
"type": "module"
to my package.json
. This adjustment led to a new error message:
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
Continuing my research, I explored different solutions such as:
Changing
to"module": "commonjs"
in"module": "es2015"
tsconfig
This change caused issues when attempting to run
ts-node-dev
, resulting in the error:
.Cannot use import statement outside a module
Adding
to"moduleResolution": "node"
tsconfig
With this adjustment and the "es2015" module specified in tsconfig, all my TypeScript imports required the .JS extension. For example:
import { foo } from '../bar.js
. Not specifying the extension resulted in an error like:
. Additionally, this prevented me from usingError [ERR_MODULE_NOT_FOUND]: Cannot find module D:/...
ts-node-dev
as it threw the previously mentioned error.