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.

Absolute imports / TS Paths aliases get treated as externals

See original GitHub issue

Hi. I am trying to create a small components lib + styleguidist playground.

Playground works fine (it uses webpack), but my build is broken. I created a dist folder, npm pack-ed it, installed locally in another project.

import { Button } from "components-lib";

Module not found: Can’t resolve ‘components/Button’

My index.ts looks like this:

export { default as Button } from "components/Button";
export { default as Checkbox } from "components/Checkbox";
export { default as Icon } from "components/Icon";
export { default as SideMenu } from "components/SideMenu";

image

I get no errors during build.

My tsconfig:

{
  "include": ["src", "@types", "test"],
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "lib": ["dom", "esnext"],
    "importHelpers": true,
    "declaration": true,
    "sourceMap": true,
    "rootDir": "./",
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "*": ["src/*", "node_modules/*"]
    },
    "jsx": "react",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "isolatedModules": true,
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
lpolitocommented, Mar 2, 2020

Module not found: Can’t resolve ‘components/Button’

This could be related with how externals are configured (which I found recently): https://github.com/jaredpalmer/tsdx/issues/6#issuecomment-593146533

  1. Marks all imports as external if they are not relative / are absolute (seen here).

Try using relative imports, e.g.

export { default as Button } from "./components/Button";
export { default as Checkbox } from "./components/Checkbox";
export { default as Icon } from "./components/Icon";
export { default as SideMenu } from "./components/SideMenu";

Same goes for your static assets, assuming you’re importing them.

1reaction
lpolitocommented, Mar 3, 2020

Yeah unfortunately the rollup config has a lot to be desired in that way.

For rollup aliases appear to be possible, see: https://github.com/rollup/plugins/tree/master/packages/alias

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths. ... Note: jsconfig.json can be used when you don't use TypeScript....
Read more >
Typescript — How to solve the problem with unresolved path ...
Path aliases are defined as an array of key-value objects with the “paths”: key. Paths setting, usually require to set also the “baseUrl”...
Read more >
Refactor aliased @ imports to relative paths - Stack Overflow
Three possible solutions that rewire aliased imports to relative paths: 1. babel-plugin-module-resolver. Use babel-plugin-module-resolver ...
Read more >
Documentation - Module Resolution - TypeScript
A relative import is resolved relative to the importing file and cannot resolve to an ambient module declaration. You should use relative imports...
Read more >
jsconfig.json Reference - Visual Studio Code
... Studio Code are treated as independent units. As long as a file a.js doesn't reference a file b.ts explicitly (either using import...
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