Unable to run dev.js outside of the project folder
mobz opened this issue · comments
Describe the bug
Running the dev.js script outside the project folder results in the esm-loader failing.
To Reproduce
$ cd ~
$ ./project/oclif-cli-test/bin/dev.js
node:internal/process/esm_loader:34
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ts-node' imported from /Users/mobz/
...
Expected behavior
The dev script should run regardless of which folder it is invoked in. The non-dev script is not effected, but you must build each time before running the script which is annoying.
Environment (please complete the following information):
- MacOS 13.6.4
- zsh 5.9 (x86_64-apple-darwin22.0)
Additional context
The solution I found was adding two additional scripts to the bin folder which do a bit of path shenanigans
./bin/devsh
#!/usr/bin/env sh
CMDWD=$PWD;
path=`dirname $0`
cd `dirname $path`
CMDWD=$CMDWD node --loader ts-node/esm --no-warnings=ExperimentalWarning bin/devsh.js $@
./bin/devsh.js
import {execute} from '@oclif/core'
process.chdir(process.env.CMDWD);
await execute({development: true, dir: import.meta.url})
I believe this is arguably a problem with node and esm_loader, however i rate it as unlikely to be quickly fixed.
The solution above stores the cwd into a env variable, cd's to the project folder to allow module resolution to occur
then sets the process cwd back to the original value before running the application