Aurelia-cli (RequireJS): move node_modules to upper directory


#1

I have a few aurelia-cli based applications in the project and want to use Yarn Workspaces with them to avoid packages duplication, i.e. instead a node_modules folder in every app just have one in the project root. This approach works for Webpack apps, but RequireJS based app can’t find dependencies without full path specified (like most of aurelia-* dependencies, etc.).

Is it possible to specify a different default path for the node_modules directory in the aurelia.json file?


How do you setup your development environment for core modules?
#2

So far, I’m using the following solution, which works, but adds a lot of extra lines to the aurelia.json:
Instead of “short” dependency declaration, like "aurelia-dependency-injection" use a full form:

{
    "name": "aurelia-dependency-injection",
    "path": "../../node_modules/aurelia-dependency-injection/dist/amd",
    "main": "aurelia-dependency-injection"
}

#3

I had a look into this issue. The answer is cli doesn’t support it. If your filesystem supports symlink, you can try ln -s ../node_modules node_modules.

cli doesn’t resolve npm package path like nodejs does. It currently only looks into local node_modules folder. But nodejs does much more. nodejs tries local node_modules folder, then repeatedly tries all parent directories’ node_modules folder, if it cannot resolve a package locally. Aftercall, nodejs also tries global paths defined by NODE_PATH, and global nodejs packages (it looks like doesn’t try global npm installed packages).

UPDATE: cli actually has some code to try parents’ node_modules folder, but it doesn’t work like nodejs as cli doesn’t test whether node_modules/packageName exists. cli only tests whether node_modules exists, it unfortunately tries to use the blank local node_modules folder created by yarn workspaces.

Your use case is the parent directory’s node_modules folder.

https://nodejs.org/api/modules.html#modules_all_together

Luckily nodejs provided an api to return a resolved path of a package require.resolve('pacakageName');

I will try to make a PR to fix cli’s package resolving code using that api, instead of hard coded 'node_modules' search. This should not only improve compatibility, but also simplify cli code as it currently checks file existence manually.


#4

Forgot that current cli requires you to write lots of explicit config for dependencies.

Once you have any dependencies with "path": "../node_modules/..." in your aurelia.json, it bypasses my proposed future fix. The fix doesn’t make sense for current cli.

So I will only implement the fix in auto-tracing version of cli which doesn’t require you to maintain explicit config.


#5

Latest auto-tracing huochunpeng/cli#at15 supports yarn workspaces.

Remember to remove all your explicit dependencies config following the test guide in the PR thread.

In addition, you need to adjust the prepend section to use "../node_modules/..." as they are all explicit paths.


#6

Thank you, @huochunpeng! I will try huochunpeng/cli#at15, it looks really promising.


#7

Can’t you use the yarn workspaces nohoist option? Sure, it means that you end up with multiple node_modules directories but it’s how we’ve solved our monorepository woes while all the tooling plays catch up…