Flow: Plugin/Preset files are not allowed to export objects, only functions

Created on 19 Oct 2018  ·  6Comments  ·  Source: facebook/flow

On running ./node_modules/.bin/babel ./src -d ./lib, I get an error telling my that 'Plugin/Preset files are not allowed to export objects, only functions'.

I am fairly certain that this issue is due to Flow, as when I remove flow from my presets, the error disappears (or rather it is replaced by a different error about flow-syntax which is now not being dealt with).


My babel.config.js:

module.exports = function(api) {

  // Cache the returned value forever and don't call this function again.
  api.cache(true);

  const presets = [
    "@babel/preset-env", 
    "flow",
    "mobx", 
    "airbnb",
  ];

  return {
    presets,
  };
}

My console error:

> [email protected] build /Users/jamesmulholland/proj/composable-mobx
> babel ./src -d ./lib

Error: Plugin/Preset files are not allowed to export objects, only functions. In /Users/jamesmulholland/proj/composable-mobx/node_modules/babel-preset-flow/lib/index.js
    at createDescriptor (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
    at items.map (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPresetDescriptors (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
    at presets (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
    at mergeChainOpts (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-chain.js:315:26)
    at /Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-chain.js:278:7
    at buildRootChain (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/config-chain.js:89:20)
    at loadPrivatePartialConfig (/Users/jamesmulholland/proj/composable-mobx/node_modules/@babel/core/lib/config/partial.js:85:55)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `babel ./src -d ./lib`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.

My package.json:

{
  "name": "composable-mobx",
  "version": "0.0.1",
  "description": "A package to make dealing with MobX easier",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "./node_modules/.bin/babel ./src -d ./lib",
    "flow": "flow"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Evermind-Digital/composable-mobx.git"
  },
  "keywords": [
    "mobx"
  ],
  "author": "James Mulholland",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Evermind-Digital/composable-mobx/issues"
  },
  "homepage": "https://github.com/Evermind-Digital/composable-mobx#readme",
  "dependencies": {
    "axios": "^0.18.0",
    "camelcase-keys": "^5.0.0",
    "flow-typed": "^2.5.1",
    "lodash": "^4.17.11",
    "mobx": "^5.5.1",
    "mobx-react": "^5.3.3",
    "snakecase-keys": "^1.2.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.1.2",
    "@babel/polyfill": "^7.0.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-preset-airbnb": "^3.0.1",
    "babel-preset-flow": "^6.23.0",
    "babel-preset-mobx": "^2.0.0",
    "flow-bin": "^0.83.0"
  }
}

Most helpful comment

@mulholio This is an issue with the package mentioned in the doc not working with Babel's presets. I had the same issue and just managed to solve by installing the @babel/preset-flow and then listing @babel/preset-flow rather than flow in the presets config.

The doc is a bit misleading and also got me as it says to list "flow" as the preset arg; Also followed the same directions at first 😅
(referencing: https://flow.org/en/docs/tools/babel/)

[Edit: attempted to fix this with a PR but hadn't signed the CLA/had an awareness about that -- so can't merge that quite yet. But let me know if it doesn't fix things!]

All 6 comments

I didn't really manage to resolve this issue, but I found a workaround by just reverting to the last commit before this and then re-doing my steps.

I am fairly certain that this issue is due to Flow, as when I remove
flow from my presets, the error disappears (or rather it is replaced
by a different error about flow-syntax which is now not being dealt
with).

I’m not sure that this is a sound assessment. If you’re not dealing with
Flow syntax, then you’ll have an error at parse time. The “plugin/preset
files are not allowed to export objects, only functions” error is raised
_after_ parse time. So removing Flow simply masks the actual error by
raising another error earlier in the process.

The source of the error message is also in the Babel repository,
not the Flow repository.

@mulholio This is an issue with the package mentioned in the doc not working with Babel's presets. I had the same issue and just managed to solve by installing the @babel/preset-flow and then listing @babel/preset-flow rather than flow in the presets config.

The doc is a bit misleading and also got me as it says to list "flow" as the preset arg; Also followed the same directions at first 😅
(referencing: https://flow.org/en/docs/tools/babel/)

[Edit: attempted to fix this with a PR but hadn't signed the CLA/had an awareness about that -- so can't merge that quite yet. But let me know if it doesn't fix things!]

@rob2d suggestion solved it for me. But here only an additional info.
I'd installed 'babel-preset-flow' before, which throws error.
But after replacing it with @babel/preset-flow in package.json and in babel.config.js it works like a charm.
Thanks @rob2d

The problem is probably using a babel 6 plugin with babel 7; I've hit it by ending up with a mixed set of babel 7 and 6 packages due to dependencies. Making sure you're consistently using one or the other should help.

@mulholio This is an issue with the package mentioned in the doc not working with Babel's presets. I had the same issue and just managed to solve by installing the @babel/preset-flow and then listing @babel/preset-flow rather than flow in the presets config.

The doc is a bit misleading and also got me as it says to list "flow" as the preset arg; Also followed the same directions at first 😅
(referencing: https://flow.org/en/docs/tools/babel/)

[Edit: attempted to fix this with a PR but hadn't signed the CLA/had an awareness about that -- so can't merge that quite yet. But let me know if it doesn't fix things!]

I really don't understand how these two packages differ. But anyways your solution worked like charm. Thanks a lot 👌

Was this page helpful?
0 / 5 - 0 ratings