10

I seem to absolutely not grasp where to put root programmatic options for the babel.

If I have a monorepo and need to tell the different sub packages that they shall look upwards for my babel.config.js then I should put rootMode: "upwards" into the .babelrc of the sub packages, correct? This does not work, because of the resulting error

Error: .rootMode is only allowed in root programmatic options

Somehow I simply can't find any example of where to put/use root programmatic options... Can anyone point me to the right direction?

hurrtz
  • 1,871
  • 1
  • 19
  • 34

3 Answers3

2

Any API-related options are called programmatic options. Take a look at my discussion with the primary maintainer of Babel: https://github.com/babel/babel/discussions/14405.

It's when you specify them directly to Babel (babel.transformSync(code, programmaticOptions) or to the Babel integration you are using (e.g. babel-loader, which can pass them to its internal babel.transform call). In other words, not in presets or config files. [...]

by @nicolo-ribaudo - Babel core team.

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
1

If you are using Webpack, you need to put it there.

module: {
  [..]
  rules: [
    // Transpile ES6 Javascript into ES5 with babel loader
    {
      test: /\.jsx?$/,
      exclude: [/node_modules/, /json/],
      loader: 'babel-loader',
      options: {
        rootMode: 'upward'
      },
    },
    [..]
  ],
  [..]
},

Otherwise I had the same issue than you, I can't put it in the package.json file using the key babel.

Ser
  • 2,661
  • 23
  • 28
  • 6
    Unless of course you're not using webpack - which he may not be - I'm not using webpack and I'm encountering the same symptoms – Developer Dave Dec 17 '18 at 21:48
  • 2
    Thanks for this answer. I specifically wanted to know if the options specified in the webpack config for the babel-loader were "programmatic" options. Your answer tells me they are. – Mike Lippert Sep 29 '20 at 21:24
  • 1
    This answer is correct because `babel-loader` will call babel's API(s). Any API-related options are called programmatic options. Take a look at my discussion with the primary maintainer of Babel: https://github.com/babel/babel/discussions/14405 – NeoZoom.lua Apr 11 '22 at 13:46
  • If you're using the Babel CLI directly, pass `--root-mode upward` as [documented here](https://babeljs.io/docs/en/options). Took me a while to find this. – Steve Jul 04 '22 at 13:15
-1

I got this error using my (probably non-standard) monorepo setup where I have top-level subdirectories for each of my packages. No top-level package. When I upgraded to Babel 7, my Jest tests were no longer transforming packages that were yarn linked into the package where I was running Jest.

I added a top-level babel.config.js as part of Babel's monorepo instructions. I had rootMode: "upwards" in these three places:

  • ui-package/webpack.config.js for transforming the app.

  • ui-package/babel-jest.js for the tests, where it appeared like:

    module.exports = require("babel-jest").createTransformer({
      rootMode: "upward",
    })
    

    and was referenced from jest.config.js in that same dir like:

    transform: {
      "^.+\\.jsx?$": "./babel-jest.js",
    },
    
  • And in /babel.config.js, the newly added top-level babel conf file.

Removing it from the last one removed the error.

Carl G
  • 17,394
  • 14
  • 91
  • 115