Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve alias in Parceljs?

I'm migrating from Webpack to Parcel and I use folder aliases like this in my component:

import * as authActions from 'actions/authActions';

I get this error: Cannot find module 'actions/authActions'

The weird thing is, it only shows when using build, it works in development mode but not in production.

I've set up my aliases in package.json like the docs say:

{
  "scripts: {
      "build-client": "parcel build client/index.html dist/client",
      "build-server": "parcel build server/index.js --target node -d dist/server",
      "build-test": "yarn build-server && node ./dist/server/index.js"
  },
  "alias": {
      "actions": "./client/actions"
  }
}

It's a server-side rendered app, I'm importing the component in different places and I can't use the default Parcel root, because it's relative to the entry file.

How can I get this to properly resolve aliases?

like image 661
Vlady Veselinov Avatar asked Jan 05 '19 14:01

Vlady Veselinov


1 Answers

This question was asked when parcel1 was the current version, so for anyone arriving here in the future, it's worth pointing out that Parcel 2 supports this through glob aliases

Your package.json would include an entry like this:

{
  "alias": {
    "actions/*": "./client/actions/$1"
  }
}

Then, to import all the exports of ./client/actions/authActions, you could write:

import * as authActions from "actions/authActions";
like image 195
Andrew Stegmaier Avatar answered Nov 01 '22 15:11

Andrew Stegmaier