question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested Module Resolution?

See original GitHub issue

I’m trying to figure out how to get nested module resolution working with this project.

What I mean is modifying the structure to a more real-world example with multiple nested modules, in a domain driven architecture where all our exports are in a domain and referenced by e.g. import { sleep } from '@quramy/x-core/messages/core'

Structure:

- x-cli
-- src
--- main.ts
- x-core
-- src
--- messages
---- core
----- sleep
------ sleep.ts
------ sleep-and-snore.ts
------ index.ts
----- wake
------ wake.ts
------ wake-and-eat.ts
------ index.ts (exports wake and wake-and-eat)

I have modified /tsconfig.json: and then updated main.ts:

{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "baseUrl": "./packages",
    "paths": {
      "@quramy/x-core/messages/*": ["./x-core/src/messages/*"],
      "@quramy/*": ["./*/src"]
    }
  }
}
// main.ts (this works fine in the IDE, just doesn't build...)
import { sleep, sleepAndSnore } from '@quramy/x-core/messages/core'

export function cli() {
  sleep();
  sleepAndSnore()
  return Promise.resolve(true);
}
cli()

yarn prepare then gives me this error: src/main.ts(1,38): error TS2307: Cannot find module ‘@quramy/x-core/messages/core’.

If I add the paths/baseUrl to the tsconfig.build.json, it will build, but it doesn’t run because it can’t find the module.

I also get the "source files not under the same rootDir message if I have that compilerOption set under this case: TS6059: File '/home/bthompson/source/lerna-yarn-workspaces-example/packages/x-core/src/messages/core/index.ts' is not under 'rootDir' '/home/bthompson/source/lerna-yarn-workspaces-example/packages/x-cli/src'. 'rootDir' is expected to contain all source files.

Any help would be greatly appreciated!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:7

github_iconTop GitHub Comments

1reaction
xiaoyu-tamucommented, Jan 2, 2019

If I add the paths/baseUrl to the tsconfig.build.json, it will build, but it doesn’t run because it can’t find the module.

I solved this by using module-alias package.

import { addAlias } from 'module-alias';
import { join } from 'path';

if (process.env.NODE_ENV === 'development') {
  addAlias('@scope/common',  join(__dirname, '../../common/build'));
}
0reactions
osdiabcommented, Nov 26, 2018

@dgreene1 Still working on it, should be out soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Dependencies in Frontend - DEV Community ‍ ‍
In short, automatic module resolution that prefers nesting may be dangerous for frontend. We care about loading and parsing performance ...
Read more >
Path resolution for nested recursive modules - cs.ioc.ee
Path resolution for nested recursive modules ... In this paper, we address one such problem, namely resolution of path references. Paths.
Read more >
Understanding npm dependency resolution - Medium
This command resolves all of the packages dependencies on Module B v2.0 by redirecting them to the top level copy of Module B...
Read more >
Module Resolution or Import Alias: The Final Guide - Raul Melo
Module resolution or import alias is a way we can emulate the same way we import node_modules but with our internal code.
Read more >
Overriding nested dependencies in NPM - Wisdom Geek
Overriding nested dependencies in NPM ... If we wanted package B to be resolved as 2.0..0 when specified as a dependency of Package...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found