Search code examples
javascriptobjectbabeljses6-modulescommonjs

InterlliJ: CommonJS exports default object properties not accessible via dot notation


Given following content of an import...

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _default = {
  SOME_CONSTANT: 'Some constant text'
};
exports["default"] = _default;

...transpiled from an original ES6 export via babel...

export default {
  SOME_CONSTANT: 'Some constant text'
}

...why would my IDE (PHPStorm / IntelliJ) report the following (dot notation) as unresolved variable...?

import textConstants from 'package-name/path/to/textConstants'

console.log(textConstants.SOME_CONSTANT)

If I convert it to (bracket notation)...

console.log(textConstants["SOME_CONSTANT"])

...inspections get satisfied, no warning is showing.

Why?


Solution

  • It looks like adding...

    { "modules": false }
    

    ...to my .babelrc file as an option to the "@babel/preset-env" preset does the trick.

    So the content of my .babelrc file is...

    {
      "presets": [
        ["@babel/preset-env", { "modules": false }]
      ]
    }
    

    ...while my dev dependencies in package.json file are...

    {
      "devDependencies": {
        "@babel/cli": "^7.15.4",
        "@babel/core": "^7.15.5",
        "@babel/preset-env": "^7.15.6"
      }
    }