Parcel: Parcel 2: Absolute path imports

Created on 21 Nov 2019  ยท  6Comments  ยท  Source: parcel-bundler/parcel

โ” 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

Question

Most helpful comment

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

All 6 comments

~/src/...

Using a tilde should work

@mischnic Thanks! Is there another way without changing project's imports?

Currently not

You can write a custom resolver plugin that supports this.

I'm fairly sure we do have a resolver for TypeScript specific stuff on the roadmap, so this should be supported someday...

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

@a-x- Worked like a charm, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Niggler picture Niggler  ยท  3Comments

algebraic-brain picture algebraic-brain  ยท  3Comments

dotdash picture dotdash  ยท  3Comments

philipodev picture philipodev  ยท  3Comments

oliger picture oliger  ยท  3Comments