babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

when switching to ESM modules, this package no longer works

Open Arro opened this issue 4 years ago • 2 comments

Once you switch your package.json to type: module, usage of the this package breaks, including utils such as babel-node.

Lets say your .babelrc.cjs looks like this:

module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        useBuiltIns: "usage",
        corejs: {
          version: 3,
          proposals: true
        },
        targets: {
          node: "14"
        }
      }
    ]
  ],
  plugins: [
    [
      "module-resolver",
      {
        alias: {
          src: "./src"
        },
        extensions: [".js"]
      }
    ]
  ]
}

If you have a file like src/nested/very/deep.js and you want to load it in another file like src/nested-elsewhere/also/very/deep.js

$ babel-node src/nested-elsewhere/also/very/deep.js

results in:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'src' imported from /Users/myuser/Code/myproject/src/nested-elsewhere/also/very/deep.js

To reaffirm, this only happens once switching to ESM modules.

Arro avatar Oct 30 '21 00:10 Arro

I'm getting the same. I spotted this on a competitor's issues list, which may indicate the cause (https://github.com/ilearnio/module-alias/issues/59).

kodikos avatar Feb 10 '22 13:02 kodikos