1

I have project in typescript. I am trying to debug with preLaunchTask to transpile typescript into js in folder build.

this is my tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017", "es2019", "esnext.asynciterable"],
    "sourceMap": true,
    "outDir": "./build",
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": false,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "src",
    "paths": {
      "*": ["./*"]
    },
    "types": ["node"],
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "allowJs": true,
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.ts"]
}

this is my launch.json

    "version": "0.2.0",
    "configurations": [
      {
        "type": "node",
        "request": "launch",
        "name": "Debug TypeScript in Node.js",
        "preLaunchTask": "typescript",
        "program": "${workspaceFolder}/api/src/index.ts",
        "cwd": "${workspaceFolder}",
        "protocol": "inspector",
        "outFiles": [
          "${workspaceFolder}/api/build/**/*.js"
        ],
        "sourceMaps": true
      }
    ]
  }

and this is my tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "typescript",
            "tsconfig": "api/tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ],
            "group": "build",
            "label": "typescript"
        }
    ]
}

I am getting

C:\Program Files\nodejs\node.exe .\api\build\index.js
Uncaught Error: Cannot find module 'database/createConnection

createConnection.js is located in build/database

Clearly I have something with paths, because __importDefault(require("database/createConnection")); this is line of code in index.js but not quite sure where to look at. Can you help me with this issue? I have found this similar topic cannot find typescript module . It seems that transpilling typescript didn't add base path to te imports.

EDIT: I have tried install module-alias and set in package.json

 "_moduleAliases": {
    "*": "./"
  }

then I added this line on beginning of index.js

require('module-alias/register');

but it doesn't work... I am also experimenting with link-module-alias but can't make symlink * work. I need all paths with default prefix ./

hocu pocs
  • 59
  • 1
  • 6
  • Can you try using require("./database/createConnection") instead ? (just added ./ at the beginning of path) – Paskal Arzel Nov 24 '20 at 17:08
  • It works but I need it to add recursively inside all files, must be way to make it automatically from typescript conversion. – hocu pocs Nov 24 '20 at 17:11
  • Then, using require.main.require could help you, so you would be able to have the same route for all your files, however, it's not exactly what you wanted. Hope it can help while you are waiting for an answer :) – Paskal Arzel Nov 24 '20 at 17:18
  • How I can use require.main.require? – hocu pocs Nov 24 '20 at 18:09
  • There is nothing to add, just replacing require by require.main.require will work – Paskal Arzel Nov 25 '20 at 10:14

0 Answers0