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.

Parcel 2: Absolute path imports

See original GitHub issue

❔ Question

How can I configure Parcel resolving absolute-like imports? I have a typescript project with imports relative to project’s root folder, e.g. import App from 'src/app/App' (having App.tsx inside of src/app folder).

When trying to run parcel serve src/index.tsx I’m getting Error: Cannot find module 'src/app/App' from '/projects/projectFolder/src'

While tsc -p tsconfig.json works smoothly.

I’m considering switching from Webpack to Parcel, but changing our imports from src/... to something like /src/... or ~/src/... to overcome this problem seems as overhead.

Below I provided simplified project settings.

Thanks for any feedback!

🔦 Context

Project structure

├── package.json ├── src │ ├── index.tsx │ └── app │ ├── App.tsx └── tsconfig.json

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "allowJs": true,
    "noEmit": true,
    "moduleResolution": "node",
    "module": "esnext",
    "target": "es6",
    "jsx": "react",
    "experimentalDecorators": true,
    "noUnusedLocals": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "lib": ["dom", "es2017"],
    "baseUrl": ".",
    "paths": {
      "*": ["*"]
    }
  },
  "exclude": ["node_modules/**/*", "src/__tests__/*", "__generated__/*"]
}

package.json:

{
  "name": "app",
  "license": "MIT",
  "version": "1.0.0",
  "main": "dist/main/index.js",
  "source": "src/index.tsx",
   "dependencies": ...
}

src/index.tsx:

import 'react-hot-loader';

import React from 'react';
import { render } from 'react-dom';
import App from 'src/app/App';

render(<App />, document.getElementById('app'));

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-alpha.3.1
Node 12.11.1
Yarn 1.19.1
Operating System MacOS Mojave 10.14.6

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
a-x-commented, Nov 22, 2019

you can use "alias": { "src": "./src" } also

4reactions
mischniccommented, Nov 21, 2019

~/src/…

Using a tilde should work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency resolution - Parcel
It is responsible for turning a dependency specifier into an absolute path that can be loaded from the file system. In addition to...
Read more >
How does import with tilde (~) work in parcel-2? - Stack Overflow
VS Code respects my tsconfig and resolves tilde imports as expected but the parcel command complains in the terminal.
Read more >
Setting up absolute paths in Parcel and TypeScript
Absolute paths are very useful. Instead of importing modules using relative paths, first descending to the root level and then going up ...
Read more >
Module Resolution - Parcel
¶ Absolute Paths ... /foo resolves foo relative to the project root. ¶ Tilde Paths. ~/foo resolves foo relative to nearest node_modules directory,...
Read more >
Next.js Tutorial - 59 - Absolute Imports & Module Paths
Courses - https://learn.codevolution.dev/⚡️ Checkout Taskade! https://www.taskade.com/ Support UPI - https://support.codevolution.dev/  ...
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