Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging fails in TS files with any imports #69

Open
josephemorgan opened this issue Apr 21, 2024 · 5 comments
Open

Debugging fails in TS files with any imports #69

josephemorgan opened this issue Apr 21, 2024 · 5 comments

Comments

@josephemorgan
Copy link

The Problem

Unable to debug anything that has an ESM import (which is the standard for writing TS as far as I'm aware).

Debugging works in single-file typescript programs. Once there's an ESM module import, the following error appears in the REPL and the debugger disconnects:

(node:69889) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
Uncaught SyntaxError /home/joe/dev/hello_world/index.mts:1
import { logMessage } from "./some_module.mjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (internal/vm:76:18)
    at wrapSafe (internal/modules/cjs/loader:1283:20)
    at Module._compile (internal/modules/cjs/loader:1328:27)
    at Module._extensions..js (internal/modules/cjs/loader:1422:10)
    at Module.load (internal/modules/cjs/loader:1203:32)
    at Module._load (internal/modules/cjs/loader:1019:12)
    at executeUserEntryPoint (internal/modules/run_main:128:12)
    at <anonymous> (internal/main/run_main_module:28:49)

What I tried

Changing type to module in package.json changes the error in the REPL to the following:

Uncaught TypeError TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/joe/dev/hello_world/index.ts
    at __node_internal_captureLargerStackTrace (internal/errors:496:5)
    at NodeError (internal/errors:405:5)
    at getFileProtocolModuleFormat (internal/modules/esm/get_format:136:11)
    at defaultGetFormat (internal/modules/esm/get_format:182:36)
    at defaultLoad (internal/modules/esm/load:101:20)
    at load (internal/modules/esm/loader:417:13)
    at moduleProvider (internal/modules/esm/loader:288:22)
    at ModuleJob (internal/modules/esm/module_job:63:26)
    at #createModuleJob (internal/modules/esm/loader:312:17)
    at getJobFromResolveResult (internal/modules/esm/loader:265:34)
    at getModuleJob (internal/modules/esm/loader:251:17)
    at processTicksAndRejections (internal/process/task_queues:95:5)
    --- await ---
    at runMainESM (internal/modules/run_main:91:21)
    at executeUserEntryPoint (internal/modules/run_main:124:5)
    at <anonymous> (internal/main/run_main_module:28:49)

The warning suggests changing the file extension to mjs, which doesn't make sense, tried changing to 'mts' instead an got the same error as initially in the REPL

Tried switching to commonjs module syntax

No combination of ts/mts extensions or "type" = "module" in package.json would allow the debugger to run without a similar message in the repl.

My config is basically copy/pasted from the docs here,

		local dap = require("dap")

		require("dap-vscode-js").setup({
			debugger_path = "/home/joe/.config/nvim/vscode-js-debug",
			adapters = { "pwa-node", "pwa-chrome", "pwa-msedge", "node-terminal", "pwa-extensionHost" }, -- which adapters to register in nvim-dap
			log_file_path = "/home/joe/.config/nvim/logs.log",
			log_file_level = vim.log.levels.TRACE,
		})

		dap.configurations["typescript"] = {
			{
				type = "pwa-node",
				request = "launch",
				name = "Launch file",
				program = "${file}",
				cwd = "${workspaceFolder}",
			},
			{
				type = "pwa-node",
				request = "attach",
				name = "Attach",
				processId = require("dap.utils").pick_process,
				cwd = "${workspaceFolder}",
			},
		}

vscode-js-debug was cloned and built today, as were basically all of the nvim plugins I'm using.

nvim: 0.9.5

Reproduction

I've set up the most basic, default Hello World typescript project I can image: https://github.com/josephemorgan/ts_helloworld

@josephemorgan
Copy link
Author

I'm wondering if this issue is related, until I opened the REPL, the the only message I was seeing was identical to what's described here:
#48 (comment)

@mikalcallahan
Copy link

I'm having this same issue on a NestJS project. @josephemorgan did you ever find a workaround?

@josephemorgan
Copy link
Author

I'm having this same issue on a NestJS project. @josephemorgan did you ever find a workaround?

I haven't yet, and I have spent literally weeks trying. Since posting this issue, I've spent a few hours every weekend trying everything I can think of or can find in a video or guide.

It's making wonder if anyone has a config where debugging works in a typescript nest application.

@mikalcallahan
Copy link

@josephemorgan I was able to get it working without this plugin but using LazyVim and Mason.

Here's the config that worked for me:

      {
        name = "Launch NestJS (Debug Mode)",
        type = "pwa-node",
        request = "launch",
        cwd = vim.fn.getcwd(),
        runtimeExecutable = "npm", -- Specify npm as the runtime executable
        args = { "run", "start:debug" }, -- Pass the run command and script name as arguments
        sourceMaps = true,
        protocol = "inspector",
        console = "integratedTerminal",
        outFiles = { "${workspaceFolder}/dist/**/*.js" },
        skipFiles = {
          "${workspaceFolder}/node_modules/**/*.js",
          "<node_internals>/**",
        },
      },

@josephemorgan
Copy link
Author

@josephemorgan I was able to get it working without this plugin but using LazyVim and Mason.

Here's the config that worked for me:

      {
        name = "Launch NestJS (Debug Mode)",
        type = "pwa-node",
        request = "launch",
        cwd = vim.fn.getcwd(),
        runtimeExecutable = "npm", -- Specify npm as the runtime executable
        args = { "run", "start:debug" }, -- Pass the run command and script name as arguments
        sourceMaps = true,
        protocol = "inspector",
        console = "integratedTerminal",
        outFiles = { "${workspaceFolder}/dist/**/*.js" },
        skipFiles = {
          "${workspaceFolder}/node_modules/**/*.js",
          "<node_internals>/**",
        },
      },

Was wondering if it worked in LazyVim, I'll have to check out their config and see if I can replicate the DAP part of it.

Thanks for updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants