英文:
VSCode debugging NodeJs project in TypeScript with modules - SyntaxError: Cannot use import statement outside a module
问题
I am having an issue with VSCode to start debugging a NodeJs project in Typescript with modules in a standard test boilerplate project. I am running in a vicious circle of 2 VSCode errors:
SyntaxError: Cannot use import statement outside a module
and after adding entry to file: package.json
"type": "module",
I get error:
Uncaught TypeError TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\data\projects\project-a01\src\main.ts
In terminal, the app runs fine! So the issue is with VSCode or I am missing something in my launch.json file.
Here are the steps I am taking to create the project:
OS: Windows 11
Global installs:
+-- node 19.2.0
+-- npm 8.19.3
+-- @jessety/pm2-logrotate@2.7.1
+-- @nestjs/cli@9.1.8
+-- @types/node@18.11.18
+-- @typescript-eslint/eslint-plugin@5.48.0
+-- @typescript-eslint/parser@5.48.0
+-- eslint@8.31.0
+-- node-windows@1.0.0-beta.6
+-- pm2@5.2.2
+-- rimraf@3.0.2
+-- ts-node@10.9.1
`-- typescript@4.9.4
Project creation commands:
mkdir C:\data\projects\project-a01
cd C:\data\projects\project-a01
mkdir .vscode
mkdir src
mkdir src\functions
npm init -y
npm install typescript --save-dev
npm install ts-node --save-dev
npm install @types/node --save-dev
npx tsc --init --outDir dist
npm init @eslint/config
This quickly, beautifully creates a NodeJs, TypeScript project with the following - slightly edited:
package.json:
{
"name": "project-a01",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx rimraf dist && tsc",
"start": "npm run build && node dist/main.js",
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint": "^8.31.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
src/main.ts
import { test } from "./functions/test.function";
function launchApp() {
console.log('Hello');
test();
}
launchApp();
src\functions\test.function.ts
export function test() {
console.log('Hello2');
}
Commands to compile and run:
tsc
node dist/main.js
and we get:
Hello
Hello2
All works fine!
NOW to VSCode - file: .vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "VSC-debug",
"type": "node",
"request": "launch",
"skipFiles": [
"<node_internals>/"
],
"program": "${workspaceFolder}/src/main.ts",
"outFiles": [
"${workspaceFolder}/dist//*.js"
],
"sourceMaps": true
}
]
}
. . . and when trying to launch in VSCode one of the 2 errors pops up - depending on the condition of adding/removing "type": "module" from package.json as described at the beginning.
The question is: How to fix these errors and get VSCode to run this basic project in debug mode and allow debugging with code breaks?
英文:
I am having an issue with VSCode to start debugging a NodeJs project in Typescript with modules in a standard test boilerplate project.
I am running in a vicious circle of 2 VSCode errors:
SyntaxError: Cannot use import statement outside a module
and after adding entry to file: package.json
"type": "module",
I get error:
Uncaught TypeError TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\data\projects\project-a01\src\main.ts
In terminal, the app runs fine !
So the issue is with VSCode or I am missing something in my launch.json file.
Here are the steps I am taking to create the project:
OS: Windows 11
Global installs:
+-- node 19.2.0
+-- npm 8.19.3
+-- @jessety/pm2-logrotate@2.7.1
+-- @nestjs/cli@9.1.8
+-- @types/node@18.11.18
+-- @typescript-eslint/eslint-plugin@5.48.0
+-- @typescript-eslint/parser@5.48.0
+-- eslint@8.31.0
+-- node-windows@1.0.0-beta.6
+-- pm2@5.2.2
+-- rimraf@3.0.2
+-- ts-node@10.9.1
`-- typescript@4.9.4
Project creation commands:
mkdir C:\data\projects\project-a01
cd C:\data\projects\project-a01
mkdir .vscode
mkdir src
mkdir src\functions
npm init -y
npm install typescript --save-dev
npm install ts-node --save-dev
npm install @types/node --save-dev
npx tsc --init --outDir dist
npm init @eslint/config
This quickly, beautifully creates a NodeJs, TypeScript project with the following - slightly edited:
package.json:
{
"name": "project-a01",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "npx rimraf dist && tsc",
"start": "npm run build && node dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"eslint": "^8.31.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
src/main.ts
import { test } from "./functions/test.function";
function launchApp() {
console.log('Hello');
test();
}
launchApp();
src\functions\test.function.ts
export function test() {
console.log('Hello2');
}
Commands to compile and run:
tsc
node dist/main.js
and we get:
Hello
Hello2
All works fine !
NOW to VSCode - file: .vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "VSC-debug",
"type": "node",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/main.ts",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"sourceMaps": true
}
]
}
. . . and when trying to launch in VSCode one of the 2 errors pops up - depending on the condition of adding/removing "type": "module" from package.json as described at the beginning.
The question is: How to fix these errors and get VSCode to run this basic project in debug mode and allow debugging with code breaks ?
答案1
得分: 3
以下是翻译好的内容:
所以在进一步研究后,以下是使VSCode在TypeScript项目上开始工作的步骤。
tsconfig.json - 添加:
"rootDir": "./src",
"moduleResolution": "node",
"sourceMap": true,
.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "VSC-debug",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}\\src\\main.ts",
"preLaunchTask": "npm: build",
"sourceMaps": true,
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
对于那些感兴趣的人,上述问题中的步骤和本答案中的调整将为我们提供一个漂亮而简单的:
NodeJs - TypeScript 项目模板 - 在VSCode中可行。
英文:
So after more research, here are the steps that made VSCode start working on the TypeScript project.
tsconfig.json - add:
"rootDir": "./src",
"moduleResolution": "node",
"sourceMap": true,
.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "VSC-debug",
"type": "node",
"request": "launch",
"program": "${workspaceFolder}\\src\\main.ts",
"preLaunchTask": "npm: build",
"sourceMaps": true,
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
For those interested, the steps in the above question and the adjustment in this answer will give us a nice and simple:
NodeJs - TypeScript project boilerplate / template - workable in VSCode.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论