VSCode debugging NodeJs project in TypeScript with modules – SyntaxError: Cannot use import statement outside a module

huangapple go评论72阅读模式
英文:

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

&quot;type&quot;: &quot;module&quot;,

I get error:

Uncaught TypeError TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension &quot;.ts&quot; 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:

{
  &quot;name&quot;: &quot;project-a01&quot;,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;description&quot;: &quot;&quot;,
  &quot;main&quot;: &quot;index.js&quot;,
  &quot;scripts&quot;: {
    &quot;build&quot;: &quot;npx rimraf dist &amp;&amp; tsc&quot;,
    &quot;start&quot;: &quot;npm run build &amp;&amp; node dist/main.js&quot;,
    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
  },
  &quot;keywords&quot;: [],
  &quot;author&quot;: &quot;&quot;,
  &quot;license&quot;: &quot;ISC&quot;,
  &quot;devDependencies&quot;: {
    &quot;@types/node&quot;: &quot;^18.11.18&quot;,
    &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^5.48.0&quot;,
    &quot;@typescript-eslint/parser&quot;: &quot;^5.48.0&quot;,
    &quot;eslint&quot;: &quot;^8.31.0&quot;,
    &quot;ts-node&quot;: &quot;^10.9.1&quot;,
    &quot;typescript&quot;: &quot;^4.9.4&quot;
  }
}

tsconfig.json

	{
	  &quot;compilerOptions&quot;: {
		&quot;target&quot;: &quot;es2016&quot;,
		&quot;module&quot;: &quot;commonjs&quot;,
		&quot;outDir&quot;: &quot;dist&quot;,
		&quot;esModuleInterop&quot;: true,
		&quot;forceConsistentCasingInFileNames&quot;: true,
		&quot;strict&quot;: true,
		&quot;skipLibCheck&quot;: true
	  }
	}

src/main.ts

import { test } from &quot;./functions/test.function&quot;;

function launchApp() {
  console.log(&#39;Hello&#39;);
  test();
}
launchApp();

src\functions\test.function.ts

export function test() {
  console.log(&#39;Hello2&#39;);
}

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

{
  &quot;version&quot;: &quot;0.2.0&quot;,
  &quot;configurations&quot;: [
    {
      &quot;name&quot;: &quot;VSC-debug&quot;,
      &quot;type&quot;: &quot;node&quot;,
      &quot;request&quot;: &quot;launch&quot;,
      &quot;skipFiles&quot;: [
        &quot;&lt;node_internals&gt;/**&quot;
      ],
      &quot;program&quot;: &quot;${workspaceFolder}/src/main.ts&quot;,
      &quot;outFiles&quot;: [
        &quot;${workspaceFolder}/dist/**/*.js&quot;
      ],
      &quot;sourceMaps&quot;: 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:

&quot;rootDir&quot;: &quot;./src&quot;,
&quot;moduleResolution&quot;: &quot;node&quot;,
&quot;sourceMap&quot;: true, 

.vscode\launch.json

{
  &quot;version&quot;: &quot;0.2.0&quot;,
  &quot;configurations&quot;: [
    {
      &quot;name&quot;: &quot;VSC-debug&quot;,
      &quot;type&quot;: &quot;node&quot;,
      &quot;request&quot;: &quot;launch&quot;,
      &quot;program&quot;: &quot;${workspaceFolder}\\src\\main.ts&quot;,
      &quot;preLaunchTask&quot;: &quot;npm: build&quot;,
      &quot;sourceMaps&quot;: true,
      &quot;smartStep&quot;: true,
      &quot;internalConsoleOptions&quot;: &quot;openOnSessionStart&quot;,
      &quot;outFiles&quot;: [
          &quot;${workspaceFolder}/dist/**/*.js&quot;
      ]
    }
  ]
}

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.

huangapple
  • 本文由 发表于 2023年1月7日 17:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75039234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定