How to make Visual Studio Code recognize the typescript path alias - Stack Overflow

Today I added typescript alias with my project, this is the tsconfig.json:{"compilerOptions"

Today I added typescript alias with my project, this is the tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "esnext",
    "moduleResolution": "bundler",
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "baseUrl": ".",
    "paths": {
      "@controllers/*": ["src/controllers/*"],
      "@websocket/*": ["src/websocket/*"]
    },
    "lib": ["DOM", "ES2021.String"],
    "sourceMap": true
  },
  "include": ["src/**/*"],
  "files": ["src/app.ts"],
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  },
  "plugins": [{ "transform": "typescript-transform-paths" }]
}

This is the import look like:

import { initialize } from "@websocket/entry/init.js";

but when I start the visual studio code debugging, shows error:

/Users/xiaoqiangjiang/.nvm/versions/node/v16.14.2/bin/node -r tsconfig-paths/register ./dist/app.js --inspect
Uncaught Error Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@websocket/entry' imported from /Users/xiaoqiangjiang/source/reddwarf/backend/texhub-broadcast/dist/app.js
    at __node_internal_captureLargerStackTrace (<node_internals>/internal/errors:464:5)
    at NodeError (<node_internals>/internal/errors:371:5)
    at packageResolve (<node_internals>/internal/modules/esm/resolve:932:9)
    at moduleResolve (<node_internals>/internal/modules/esm/resolve:978:18)
    at defaultResolve (<node_internals>/internal/modules/esm/resolve:1080:11)
    at resolve (<node_internals>/internal/modules/esm/loader:530:30)
    at getModuleJob (<node_internals>/internal/modules/esm/loader:251:18)
    at <anonymous> (<node_internals>/internal/modules/esm/module_job:79:40)
    at link (<node_internals>/internal/modules/esm/module_job:78:36)
    --- async function ---
    at runMainESM (<node_internals>/internal/modules/run_main:51:21)
    at executeUserEntryPoint (<node_internals>/internal/modules/run_main:74:5)
    at <anonymous> (<node_internals>/internal/main/run_main_module:17:47)
errors:464
Process exited with code 1

It seems that Visual Studio Code Version: 1.97.2 (Universal) did not recognize the path alias. Am I missing something?

I have created this path alias when building project like this:

  "scripts": {
    "build": "tsc && tsc-alias"
  },

also tried change the launch.json config like this:

{
      "type": "node",
      "request": "launch",
      "name": "local debug",
      "program": "${workspaceFolder}/src/app.ts",
      // "preLaunchTask": "tsc: build - tsconfig.json",
      "runtimeArgs": [
        "-r",
        "${workspaceFolder}/node_modules/ts-node/register",
        "-r",
        "${workspaceFolder}/node_modules/tsconfig-paths/register"
      ],
      "args": ["${workspaceFolder}/src/app.ts"],
      "sourceMaps": true,
      "cwd": "${workspaceFolder}",
      "env": {
        "NODE_ENV": "development",
        "TS_NODE_PROJECT": "${workspaceFolder}/tsconfig.json"
      },
      "outFiles": ["${workspaceFolder}/dist/**/*.js"]
    }

did not fixed this issue. I also tried to change node to ts-node:

"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-node",

did not fixed this issue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745043534a4607956.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信