question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Babel7] Does not resolve module for monorepo with babel-node

See original GitHub issue

Hi there,

I have a monorepo designed like this:

- packages
-- package-1
-- package-2
babel.config.js

In packages/package-2 I have a script defined in package.json like this:

"dev": "babel-node --config-file ../../babel.config.js ./index.js",

The babel config for module-resolver is :

 const plugins = [
    [
      "module-resolver",
      {
        cwd: "packagejson",
        root: "./",
        alias: {
          "@package1": "./packages/package-1"
        }
      }
    ]
  ];

The babel config is loaded, but can’t resolve the module './packages/package-1' :

Error: Cannot find module './packages/package-1'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> ([...]/packages/package-2/index.js:1:1)
    at Module._compile (module.js:653:30)
    at Module._compile ([...]/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (module.js:664:10)
    at Object.newLoader [as .js] ([...]/node_modules/pirates/lib/index.js:88:7)
    at Module.load (module.js:566:32)

Thanks a lot !

EDIT: workaround for me : using yarn workspaces https://yarnpkg.com/lang/en/docs/workspaces/#toc-how-to-use-it

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:16

github_iconTop GitHub Comments

6reactions
Izhakicommented, Dec 21, 2018

That’s because babel does not build anything outside the current cwd. You’d need to override the ignore option to only include node_modules. This is how I do this, albeit with babel/register:

require('@babel/register')({
  // Find babel.config.js up the folder structure.
  rootMode: 'upward',

  // Since babel ignores all files outside the cwd, it does not compile sibling packages
  // So rewrite the ignore list to only include node_modules
  ignore: ['node_modules'],
});
1reaction
avallonazevedocommented, May 11, 2020

@snaerth

I had the same problem as you. I solved it by creating a specific babel.config.js for each package and extending it to the root babel.config.js.

packages
- package-1
-- utils
-- components
-- babel.config.js
- package-2
-- src
-- babel.config.js
babel.config.js

For each babel.config, I have this configuration:

module.exports = {
  extends: './../../babel.config.js',
  plugins: [
    [
      'module-resolver',
      {
        alias: {
          '@utils': './utils',
          '@components': './components',
        },
      },
    ],
  ],
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Babel preset not loading on monorepo project - Stack Overflow
Your Babel dependencies should be installed at the root package.json (since they will be shared across all packages).
Read more >
Upgrade to Babel 7
The most important change is finally switching all packages to scoped packages (the folder names in the monorepo are not changed but the...
Read more >
How to transpile node_modules with babel and webpack in a ...
In this case though, we are not transpiling between Javascript and a ... for the following caveat: It does not work when used...
Read more >
Babel 7 + TypeScript - Artsy Engineering
One of the first things Babel 7 users will notice is the package ecosystem now exists as a monorepo and all NPM modules...
Read more >
Turning a Node.js Monolith into a Monorepo without ... - InfoQ
the production build fails to run, because of Cannot find module errors;; and the import path to common-utils is more verbose than necessary....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found