oclif / core

Node.js Open CLI Framework. Built by Salesforce.

Home Page:https://oclif.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

  1. $ cd ~
  2. $ ./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