TypeError: Cannot read property 'buildMeta' of undefined

Viewed 1225

On webpack@5.72.0 (currently the latest), I started seeing:

TypeError: Cannot read property 'buildMeta' of undefined
    at HarmonyImportSpecifierDependency._getEffectiveExportPresenceLevel (/src/project/node_modules/.pnpm/webpack@5.72.0_webpack-cli@4.9.2/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:165:43)
    at HarmonyImportSpecifierDependency.getWarnings (/src/project/node_modules/.pnpm/webpack@5.72.0_webpack-cli@4.9.2/node_modules/webpack/lib/dependencies/HarmonyImportSpecifierDependency.js:176:32)
    at Compilation.reportDependencyErrorsAndWarnings (/src/project/node_modules/.pnpm/webpack@5.72.0_webpack-cli@4.9.2/node_modules/webpack/lib/Compilation.js:3132:24)
    at /src/project/node_modules/.pnpm/webpack@5.72.0_webpack-cli@4.9.2/node_modules/webpack/lib/Compilation.js:2729:28

The top frame is (source code here):

  /**
   * @param {ModuleGraph} moduleGraph module graph
   * @returns {number} effective mode
   */
  _getEffectiveExportPresenceLevel(moduleGraph) {
    if (this.exportPresenceMode !== ExportPresenceModes.AUTO)
      return this.exportPresenceMode;
    return moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
      ? ExportPresenceModes.ERROR
      : ExportPresenceModes.WARN;
  }

Why would moduleGraph.getParentModule(this) be undefined? What should normally prevent it?

I dumped out this from inside _getEffectiveExportPresenceLevel and got:

HarmonyImportSpecifierDependency {
  _parentModule: undefined,
  _parentDependenciesBlock: undefined,
  _parentDependenciesBlockIndex: -1,
  weak: false,
  optional: false,
  _locSL: 1,
  _locSC: 12241,
  _locEL: 1,
  _locEC: 12242,
  _locI: undefined,
  _locN: undefined,
  _loc: SourceLocation {
    start: Position { line: 1, column: 12241 },
    end: Position { line: 1, column: 12242 }
  },
  request: '@emotion/stylis',
  userRequest: '@emotion/stylis',
  range: [ 12241, 12242 ],
  assertions: undefined,
  sourceOrder: 4,
  ids: [ 'default' ],
  name: 'p',
  exportPresenceMode: 2,
  namespaceObjectAsContext: false,
  call: undefined,
  directImport: true,
  shorthand: false,
  asiSafe: true,
  usedByExports: undefined
}

I'm not too sure what this means. Maybe request or userRequest are a hint (@emotion/stylis). I'm not sure if the problem is more on the importing side than the imported and which one this is referring to.

Not sure why this suddenly started happening, suppose some change in package versions. Code and config unchanged lately in that area of the project.

2 Answers

The project yielding this issue was a pnpm-based monorepo with internal package dependencies using webpack build caches. I found unifying the webpack version throughout the monorepo resolved the issue.

For me, I have changed some properties of package.json file from

"webpack": "5.71.0",
"webpack-cli": "4.9.2",
"webpack-dev-server": "4.8.0"

to

"webpack": "5.65.0",
"webpack-cli": "4.9.1",
"webpack-dev-server": "4.7.1",
Related