I need to debug a library that I'm using in node_modules
. Unfortunately the library is written in Typescript. How should I force the Typescript code I edited to be recompiled?
FYI: The library is https://www.npmjs.com/package/hardhat.
CodePudding user response:
When you install an NPM package, it is already built and compiled to JavaScript.
Some .ts
files may still be visible, but there is no guarantee that you can recompile it without issues, because it may rely on configurations that aren't present in this build.
You can do a few things:
First, you can try to use your debugger to set breakpoints in the typescript files. If the library came with sourcemaps, this may work out of the box. Using the debugger you can view or edit internal values for debugging.
Second, you can edit the compiled .js
library files directly. If you just need to change a few small things, this is usually the easiest way, unless the .js
files have been minified.
Third, you can attempt to recompile it. Run npm install -g typescript
. Then find the folder inside the library folder that contains a tsconfig.json
file and open terminal/cmd in it. Run the command tsc
or npx tsc
. Alternatively add the -w
flag so it recompiles as you change the files. You may see lots of typing errors, but they don't prevent the compiler from compiling. There is no guarantee that this will work.
Fourth, you can look at the maintainer's docs on how to manually build the library from scratch. This is the "proper" way, but it may be very time-consuming if you only want to test something very quickly.