httptoolkit / httptoolkit-server

The backend of HTTP Toolkit

Home Page:https://httptoolkit.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'Cannot find module prepend-node.js' when using npx in hooked terminal

leumasme opened this issue · comments

commented

My steps to cause this error:

  • Copy existing terminal powershell command and run it in a terminal
  • Run npx tsx in my typescript/nodejs project

The error:

❯ npx tsc
node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module 'S:\Dev\my-project\..\js\prepend-node.js'
Require stack:
- internal/preload
    at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
    at mod._load (C:\Users\Temm\AppData\Local\httptoolkit-server\client\1.12.3\overrides\js\wrap-require.js:52:26)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1395:12)
    at loadPreloadModules (node:internal/process/pre_execution:621:5)
    at setupUserModules (node:internal/process/pre_execution:125:3)
    at prepareExecution (node:internal/process/pre_execution:116:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:36:3)
    at node:internal/main/run_main_module:10:1 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'internal/preload' ]
}

Node.js v18.13.0

Some extra info:

  • This happens consistently
  • Just running npx tsc to run the typescript compiler causes the error
  • My Typescript package version is 4.8.4

I can't reproduce this I'm afraid - I've just tested tsc in an intercepted terminal and it works fine.

This might be related to some specifics of how your project config is setup - can you share that so I can test this directly?

The way this works is that when node is run on your system, it actually runs this wrapper .bat file which runs the 'real' node with an extra -r "%WRAPPER_FOLDER%\..\js\prepend-node.js" argument. That WRAPPER_FOLDER variable is set within that file, relative to the path to that wrapper bat file, so I have no idea how it could become "S:\Dev\my-project". Anything unusual about your setup at all, or anything else you can think of that might cause this?

Or maybe this is related to powershell... What happens if you do the same from a cmd.exe terminal?

commented
Troubleshooting

I've now additionally tried to reduce the problem with these steps, all of which still let the error happen

  • Removing all my application code
  • Removing my tsconfig
  • Running everything from a Powershell terminal with -NoProfile enabled
  • Doing this in a different directory altogether
  • Wait what?
  • Running npx npm -v instead of npx tsc
  • Wait what?

Running npm -v and tsc -v without npx seems to work fine.
So it seems that this is not related to typescript at all but instead... npx?
Below is a terminal log, starting from a fresh cmd session.

CLI Session
Microsoft Windows [Version 10.0.19042.1288]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Temm>pwsh -NoProfile
PowerShell 7.3.3
PS C:\Users\Temm> npx npm -v
8.5.3
PS C:\Users\Temm> npm -v
8.5.3
PS C:\Users\Temm> Invoke-Expression (Invoke-WebRequest http://localhost:8001/ps-setup).Content
HTTP Toolkit interception enabled
PS C:\Users\Temm> npx npm -v
node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module 'C:\Users\Temm\..\js\prepend-node.js'
Require stack:
[...]

Node.js v18.13.0
PS C:\Users\Temm> npm -v
8.5.3

npm run <script> automatically runs the commands of the package.json script in an npx environment, so these will error even if called without npx.
You can also run npx without arguments to get into a (cmd) npx environment, where commands like npm -v will again fail.

It seems likely that the npx environment doesn't carry over the WRAPPER_FOLDER variable set in the bat script, leading to the path to prepend-node.js being incorrectly built, but I couldn't reproduce this by just setting a variable and echoing it from within npx.

commented

but I couldn't reproduce this by just setting a variable and echoing it from within npx

To amend that:
Adding an echo Within node.bat, the wrapper is %WRAPPER_FOLDER% / %~dp0 to node.bat always yields the current path of the terminal, instead of the location of the bat file, if ran through npx.
If the bat file is manually ran by just pasting its abolute path into the terminal, it correctly prints the location of the bat file. Huh!?

It seems that Powershell runs batch scripts happily, but runs them in subtly different ways to cmd.exe, and that clearly causes some problems.

I've found a way to refactor these scripts to avoid needing to query the script path entirely: 00ee967. This seems to work in my testing, and neatly sidesteps the issue. Can you test that out, and check it works for you?

commented

Yes, copying over the bat file from the commit resolves this issue for me. Thanks for your great work!