I am somewhat new to typescript, and I am working in a fork of someone else's existing repository, trying to run tests against it.
Summary
I have written a test script examples/test.ts
and am attempting to debug it using the vscode debugger. When I run the script with ts-node ./examples/test.ts
, the script executes successfully. However, when I try to run it using the vscode debugger, I get the famous SyntaxError: Cannot use import statement outside a module
. This error occurs on line 1 of my typescript test, where I first try to import {Foo} from '../dist'
, where Foo
is an object under test.
Attempted Fixes
I've been researching for a little while, the most common fix for this seems to be to set module
to commonjs
in the tsconfig.json
compilerOptions
. But, this project already has that set up correctly.
Another common fix is to set "type":"module"
in package.json
, but when I do that I cause new errors in the vscode debugger. Specifically, Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/truncated/by/me/examples/test.ts
. The fact that I can run the script normally outside the debugger makes me think it's more likely to be tsconfig.json
or similar configuration, of course I may be wrong.
Relevant config files:
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/examples/test.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["es2015", "dom"],
"allowJs": true,
"skipLibCheck": true,
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"moduleResolution": "node",
"baseUrl": "./",
"esModuleInterop": true
},
"include": ["*"],
"exclude": ["node_modules"]
}
Versions
Running VSCode 1.52.1 on up-to-date macOS. ts-node --version
gives v9.1.1
, and the project uses node --version
of v12.14.1
.