babel-plugin-module-resolver: Relative paths not correctly resolved when used with babel-register

We want to use this plugin because it is really great, but we couldn’t easily get it working our serverside-rendering app.

.babelrc

{
  "presets": [
    "es2015-loose", "stage-1", "react"
  ],
  "plugins": [
    [
      "module-alias",
      [
        { "src": "./src/js-modules/modules", "expose": "modules" }
      ]
    ]
  ]
}

services/v8/serverside.js

...
require('babel-register')({
        only: /src\/js-modules/
    });

When we now try to transform a file with import a from 'modules/abc'; we receive an error:

Error: Cannot find module '../../../../../services/v8/src/js-modules/modules/abc'

So it seems that the paths are not relative to the babelrc file, but to something else. Did we make something wrong?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 37 (23 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks @danez for reporting the issue. I believe we’ve found the root cause of this issue. Unfortunately I’m really busy this week and will be out of town this weekend, but I’ll be working on a new version of the plugin with hopefully a fix for you next week! I wanted to give you an update on this issue so you know I’m reading the tickets 😉

Gotcha, so #71 should resolve the issue… I’ll try to find some time this weekend to get this in

If any of you are trying to get this working with Ava, I came up with a solution. This works on both my local machine and Travis.

  • Babel v6.5.2
  • Ava v0.16.0
  • babel-plugin-module-resolver v2.2.0
  1. Setup your package.json like so:
"scripts": {
   "test": "BABEL_DISABLE_CACHE=1 PWD=$(pwd) NODE_ENV=test ava",
   "test:watch": "npm test -- --watch"
},

"ava": {
   "verbose": true,
   "babel": "inherit",
   "require": [
      "babel-register",
      "./test/helpers/pwd.js",
      "./test/helpers/setup-browser-env.js"
    ]
  },
  1. Change the pwd before each test (I implemented in test/helpers/pwd.js):
/**
  Note: Set the PWD to the root of the project (see package.json where we set the ENV variable)
  We need to do this to get `babel-plugin-module-resolver` working with Ava. The issue
  is that Ava changes the PWD to each test's location.
*/
process.chdir(process.env.PWD)

Yes sorry, I tried to find a way but couldn’t achieve what I wanted. Basically, inside the plugin, I’d need to know from where the configuration is loaded (ie. find the path of babelrc file), but I don’t know how to do it.

Paths inside the module-alias configuration must be relative to the configuration (which is usually the case for the users, but the plugin then transform them based on the working directory (which is wrong). That’s why I need to find a way to get the path from the configuration file.

I have just released 3.0.0-beta.1. I tested it in my own project and it seems to work fine. Released as beta for now because I want to make sure it resolves the issues for everyone, so please test and let me know it all’s good 😃

Also, with this 3.0.0-beta.1, you must also update eslint-import-resolver-babel-module to 3.0.0-beta.1, otherwise you’ll get false eslint errors.