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!
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'));
| 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
~/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!
Most helpful comment
you can use
"alias": { "src": "./src" }also