nx: NX 16.8.1 failed to compile with generatePackageJson:true

Current Behavior

When trying to build with generatePackageJson option at true, nx failed to compile.

Here is the stacktrace:

$ nx build backend-app --verbose

 >  NX   Running target build for project backend-app and 47 tasks it depends on:

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> nx run backend-app:build


 >  NX   Cannot read properties of undefined (reading 'data')


HookWebpackError: Cannot read properties of undefined (reading 'data')
    at makeWebpackError (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/HookWebpackError.js:48:9)
    at /home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/Compilation.js:3068:12
    at eval (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:23:1)
    at fn (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/Compilation.js:481:17)
    at _next0 (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:21:1)
    at eval (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:34:1)
    at eval (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/Compilation.js:503:10
    at Hook.eval [as callAsync] (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:7:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/Hook.js:18:14)
-- inner error --
TypeError: Cannot read properties of undefined (reading 'data')
    at /home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/nx/src/plugins/js/package-json/create-package-json.js:124:55
    at Array.forEach (<anonymous>)
    at findProjectsNpmDependencies (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/nx/src/plugins/js/package-json/create-package-json.js:122:33)
    at createPackageJson (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/nx/src/plugins/js/package-json/create-package-json.js:23:21)
    at /home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/@nx/webpack/src/plugins/generate-package-json-plugin.js:33:64
    at fn (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/Compilation.js:479:10)
    at _next0 (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:21:1)
    at eval (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:34:1)
    at eval (eval at create (/home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /home/fabien/gitrepos/scrapoxy/scrapoxy4-nx/node_modules/webpack/lib/Compilation.js:503:10

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Running target build for project backend-app and 47 tasks it depends on failed

   Failed tasks:
   
   - backend-app:build
   
   Hint: run the command with --verbose for more details.

Issue happens in create-package-json.js (here):

const seen = new Set();
    (_b = options.helperDependencies) === null || _b === void 0 ? void 0 : _b.forEach((dep) => {
        seen.add(dep);
        npmDeps.dependencies[graph.externalNodes[dep].data.packageName] =
            graph.externalNodes[dep].data.version;
        recursivelyCollectPeerDependencies(dep, graph, npmDeps, seen);
    });

it is regardings tslib.

graph.externalNodes is {}

Expected Behavior

We need to generate the package.json.

Package manager is standard npm (not pnpm, like in this fix)

GitHub Repo

No response

Steps to Reproduce

  1. Use version 16.8.1
  2. Build and generate package.json

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 18.17.1
   OS     : linux-x64
   npm    : 10.1.0
   
   nx (global)        : 16.8.1
   nx                 : 16.8.1
   @nx/js             : 16.8.1
   @nx/jest           : 16.8.1
   @nx/linter         : 16.8.1
   @nx/workspace      : 16.8.1
   @nx/angular        : 16.8.1
   @nx/cypress        : 16.8.1
   @nx/devkit         : 16.8.1
   @nx/eslint-plugin  : 16.8.1
   @nx/nest           : 16.8.1
   @nx/node           : 16.8.1
   @nrwl/tao          : 16.8.1
   @nx/webpack        : 16.8.1
   typescript         : 5.1.6

Failure Logs

No response

Package Manager Version

10.1.0

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

It works with version 16.7.4 (package.json is generated)

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 8
  • Comments: 30 (11 by maintainers)

Most upvoted comments

@jaqua I think I finally found the problem. I had package-lock.json in my .gitignore file as I don’t checkin this to git and generate it in Azure pipeline. I removed this line and it works fine. When I add it to .gitignore I get the error back. so I think .gitignore is used internally by Nx. Very strange but that’s what I was able to find after wasting all day.

Are people seeing the error from the same line as the OP?

npmDeps.dependencies[graph.externalNodes[dep].data.packageName]

Can you try nx reset to rebuild the graph? It looks like either the analyzeSourceFiles option is not turned on for @nx/js plugin, or the graph isn’t populated correctly. Removing the cache folder isn’t enough, since Nx Daemon is still running.

It seems something strange with package-lock.json. I created a sample app in my old repo, but still failed. Then I copied the package.json and package-lock.json from new workspace to old repo and the build of new app worked fine.