alias-hq icon indicating copy to clipboard operation
alias-hq copied to clipboard

Running node scripts with `alias-hq/init` from subdirectories fails

Open davestewart opened this issue 3 years ago • 3 comments

Originally posted by @andidev in https://github.com/davestewart/alias-hq/issues/46#issuecomment-1108262620

It looks like the solution for loading Alias HQ via a require flag in Node is causing issues when running scripts not in root project folder; package.json does not seem to be found.

This makes running scripts from WebStorm fail so we cannot still replace tsconfig-paths with alias-hq fully 😞

Screenshot of this issue reproduced with alias-hq from terminal and how same works with tsconfig-paths:

image

I am guessing this is caused by how alias-hq looks for package.json is different from tsconfig-paths, or maybe tsconfig-paths doesn't load the package.json at all since it has no settings saved there? Not sure.

Is it possible to change that in case package.json is not found it does not throw an error? Oor maybe it looks for a package.json file further up in the directory tree until it finds one?

I can provide a PR for this if it's ok

davestewart avatar Apr 25 '22 14:04 davestewart

Just looking at this now @andidev.

So Alias expects the script to be run from the same folder the package.json is in.

You say you're running the script from another folder. Can you give some more context as to where everything is and where you are running from, and why?

I'm guessing:

+- project
    +- node_modules
    +- src                     <- current directory
    |   +- playground
    |       +- playground.ts   <-- target file
    +- package.json
    +- tsconfig.json

The no-code workaround would be to run all this from the project root, no?

Any reason .bin isn't in your local PATH?

export PATH=$PATH:node_modules/.bin

Would mean less typing:

ts-node -r alias-hq/init src/playground/playground.ts

Right now I'm thinking if you want to run scripts from subfolders, I would need to:

  • look up the tree for either package.json or (ts|js)config.json
  • choose this as the root folder
  • load settings
  • load config

I've implemented a very basic file lookup locally using find-nearest-file but not changed the whole boot routine yet – and not sure I want to risk changing it right this second as I'm pretty busy.

Did the tsconfig-paths lib handle this as outlined in #46 work?

davestewart avatar Apr 25 '22 14:04 davestewart

@davestewart sorry for the late response. been super busy.

You say you're running the script from another folder. Can you give some more context as to where everything is and where you are running from, and why?

So I run all sorts of scripts for pathing database and just to simply run jobs when fixing bugs. I do that from WebStorm as seen in the image. None of these are located in the root folder since it would be a really big mess. image

And this is now failing with -r alias-hq/init but do work with -r tsconfig-paths/register.

The no-code workaround would be to run all this from the project root, no?

Yes it works running from project root. And my example command line call is just a way to reproduce the issue.

Any reason .bin isn't in your local PATH? Would mean less typing.

Thanks for the tip. But it is in my path but I have not installed ts-node globally since different projects use different version and therefore I call it directly from the node_modules in the project.

Did the tsconfig-paths lib handle this as outlined in https://github.com/davestewart/alias-hq/issues/46#issuecomment-1054152397 work?

Yes it works. And I can still use it instead of alias-hq/init from WebStorm so not a super big issue. But would be great to be able to only use one plugin for one thing and your lib is the most promising as I see it.

andidev avatar Apr 26 '22 23:04 andidev

Ok, gotcha. I'll look to fix it!

davestewart avatar Apr 27 '22 00:04 davestewart