Skip to content

[@nx/next:build executor] Generated during build step package.json file is missing information #21535

@taurusx

Description

@taurusx

Current Behavior

NextJS project contains custom package.json file and this file is imported into next.config.js.

// next.config.js
const packageJsonAppVersion = require('./package.json').version;
console.log({ packageJsonAppVersion });

When app is built, in the dist/ folder, the package.json file does not contain auto-generated information, like dependencies and scripts , but it is just a copy of project's package.json.

{
  "name": "@custom-scope/nx-bug-next-package-json",
  "version": "1.2.3"
}

Expected Behavior

App's distribution code should contain correct auto-generated package.json file:

{
  "name": "@custom-scope/nx-bug-next-package-json",
  "version": "1.2.3",
  "scripts": {
    "start": "next start"
  },
  "dependencies": {
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "next": "14.0.4",
    "typescript": "5.3.3"
  }
}

GitHub Repo

https://github.com/taurusx/nx-bug-next-package-json

Steps to Reproduce

  1. Download example repository or just start one from NX + NextJS app template: pnpx create-nx-workspace@latest --preset=next
  2. NextJS app needs to contain following changes (already set in example repo):
  • Custom package.json file inside a project (NextJS app) directory;
  • Import of package.json in next.config.js file.
  1. Build an example app and check dist/ folder's package.json file. (in example repo run pnpm nx build nx-bug-next-package-json to build an app)

Nx Report

Node   : 20.9.0
   OS     : darwin-x64
   pnpm   : 8.3.1
   
   nx                 : 17.3.1
   @nx/js             : 17.3.1
   @nx/jest           : 17.3.1
   @nx/linter         : 17.3.1
   @nx/eslint         : 17.3.1
   @nx/workspace      : 17.3.1
   @nx/devkit         : 17.3.1
   @nx/eslint-plugin  : 17.3.1
   @nx/next           : 17.3.1
   @nx/react          : 17.3.1
   @nrwl/tao          : 17.3.1
   @nx/web            : 17.3.1
   typescript         : 5.3.3

Failure Logs

No response

Package Manager Version

pnpm 8.3.1

Operating System

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

Additional Information

Bug location:

  1. @nx/next:build executor generates package.json with updatePackageJson(builtPackageJson, context);:
    https://github.com/nrwl/nx/blob/master/packages/next/src/executors/build/build.impl.ts#L83 and next line
  2. Copy step for next.config.js and its dependencies, createNextConfigFile(options, context);
    https://github.com/nrwl/nx/blob/master/packages/next/src/executors/build/build.impl.ts#L105
  3. As long as there is package.json imported inside next.config.js, the function above (step 2.) sees it as a required dependency, so it copies it over, as well. But package.json has already been generated in step 1., so it gets overwritten.

Maybe package.json should be explicitly excluded from const moduleFilesToCopy = getRelativeFilesToCopy(...) https://github.com/nrwl/nx/blob/master/packages/next/src/executors/build/lib/create-next-config-file.ts#L74 ?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions