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
- feat: Use the babelrc dirname to set the working directory (#71) Fixes #61 Now, the paths inside the configuration must be relative to the babelrc file instead of the working directory, because so... — committed to tleunen/babel-plugin-module-resolver by tleunen 8 years ago
- yarn upgrade babel-plugin-module-resolver#2.2.0 have following issue * https://github.com/tleunen/babel-plugin-module-resolver/issues/61 As a workaround, specify a version 3.0.0-beta.1 — committed to rike422/operatta-js by rike422 8 years ago
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.
v6.5.2v0.16.0v2.2.0package.jsonlike so:test/helpers/pwd.js):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.
Okay I created a test repository here: https://github.com/danez/babel-plugin-module-resolver-issue-61