nx: [Nest] Nest app build failure with generatePackageJson=true - Nx 18

Current Behavior

Nest app build fails with generatePackageJson=true option

Expected Behavior

Nest app should build without error and generate the package.json file

GitHub Repo

No response

Steps to Reproduce

  1. Create a new workspace with nx 18
  2. Install @nx/nest plugin
  3. Create a nest app
  4. Update webpack.config.js to include genetarePackageJson: true
  5. Run build command to build your app

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 20.11.0
   OS     : darwin-x64
   npm    : 10.4.0
   
   nx (global)        : 18.0.2
   nx                 : 18.0.2
   @nx/js             : 18.0.2
   @nx/jest           : 18.0.2
   @nx/linter         : 18.0.2
   @nx/eslint         : 18.0.2
   @nx/workspace      : 18.0.2
   @nx/devkit         : 18.0.2
   @nx/eslint-plugin  : 18.0.2
   @nx/nest           : 18.0.2
   @nx/node           : 18.0.2
   @nrwl/tao          : 18.0.2
   @nx/web            : 18.0.2
   @nx/webpack        : 18.0.2
   typescript         : 5.3.3
   ---------------------------------------
   Community plugins:
   @nx-tools/nx-container : 5.2.0


### Failure Logs

```shell
sandbox on  main [!+?] via  v20.11.0 took 4s 
❯ nx run api:build --skip-nx-cache

 >  NX   --skip-nx-cache disables the connection to Nx Cloud for the current run.

   The remote cache will not be read from or written to during this run.


> nx run api:build

[webpack-cli] HookWebpackError: ENOENT: no such file or directory, open 'package-lock.json'
    at makeWebpackError (/Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/HookWebpackError.js:48:9)
    at /Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:3075:12
    at eval (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at fn (/Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:481:17)
    at _next0 (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at eval (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
    at Hook.eval [as callAsync] (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:516:46
    at /Users/gperdomor/Downloads/sandbox/node_modules/copy-webpack-plugin/dist/index.js:909:9
-- inner error --
Error: ENOENT: no such file or directory, open 'package-lock.json'
    at readFileSync (node:fs:453:20)
    at createLockFile (/Users/gperdomor/Downloads/sandbox/node_modules/nx/src/plugins/js/lock-file/lock-file.js:122:43)
    at /Users/gperdomor/Downloads/sandbox/node_modules/@nx/webpack/src/plugins/generate-package-json-plugin.js:39:138
    at fn (/Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:479:10)
    at _next0 (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at eval (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
    at Hook.eval [as callAsync] (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:516:46
    at /Users/gperdomor/Downloads/sandbox/node_modules/copy-webpack-plugin/dist/index.js:909:9
caused by plugins in Compilation.hooks.processAssets
Error: ENOENT: no such file or directory, open 'package-lock.json'
    at readFileSync (node:fs:453:20)
    at createLockFile (/Users/gperdomor/Downloads/sandbox/node_modules/nx/src/plugins/js/lock-file/lock-file.js:122:43)
    at /Users/gperdomor/Downloads/sandbox/node_modules/@nx/webpack/src/plugins/generate-package-json-plugin.js:39:138
    at fn (/Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:479:10)
    at _next0 (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at eval (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
    at Hook.eval [as callAsync] (eval at create (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/gperdomor/Downloads/sandbox/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/gperdomor/Downloads/sandbox/node_modules/webpack/lib/Compilation.js:516:46
    at /Users/gperdomor/Downloads/sandbox/node_modules/copy-webpack-plugin/dist/index.js:909:9
Warning: run-commands command "webpack-cli build --node-env=production" exited with non-zero status code
 ———————————————————————————————————————————————————————————————————————————————————————————————————————————————


### Package Manager Version

npm 10.4.0

### Operating System

- [X] macOS
- [ ] Linux
- [ ] Windows
- [ ] Other (Please specify)

### Additional Information

_No response_

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Reactions: 9
  • Comments: 27 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@bddy i changed it a bit to be more safer and include all package locks, also it will always copy to the directory of the targets’ project:

{
  "targets": {
    "sync-lock": {
      "executor": "nx:run-commands",
      "options": {
        "command": "npx shx cp *-lock.* {projectRoot}"
      }
    }  
}

Our temp solution until a fix is available:

  // project.json
  "targets": {
    "sync-package-lock": {
      "executor": "nx:run-commands",
      "options": {
        "commands": [
          "# TODO: delete this target once https://github.com/nrwl/nx/issues/21651 is fixed",
          "npx cpy package-lock.json <path-to-project-root> --flat"
        ]
      }
    }
  }
  // root package.json
  "scripts": {
    "postinstall": "npx nx run-many --target sync-package-lock --all",
  }

@ericbf i thought about dependsOn inside the build target as well but then figured i only really wanted to sync after npm i. Anyways, with caching and everything generic, that’s an awesome solution we hopefully won’t need anymore with the next update 😄

I dont know where to put the "generatePackageJson": true now

use the project-specific webpack.config.js

const { NxWebpackPlugin } = require('@nx/webpack');
const { join } = require('path');

module.exports = {
  output: {
    path: join(__dirname, '../../dist/apps/your-service'),
  },
  plugins: [
    new NxWebpackPlugin({
      target: 'node',
      compiler: 'tsc',
      main: './src/main.ts',
      tsConfig: './tsconfig.app.json',
      assets: ['./src/assets'],
      optimization: false,
      outputHashing: 'none',
      generatePackageJson: true,
      sourceMap: true,      
    }),
  ],
};

fixed manually editing the file node_modules\nx\src\plugins\js\lock-file\lock-file.js edited the row 122: const content = (0, fs_1.readFileSync)(getLockFileName(packageManager), 'utf8'); to const content = (0, fs_1.readFileSync)(path_1.join(workspace_root_1.workspaceRoot, getLockFileName(packageManager)), 'utf8');

I’m unable to find the actual code in the repository, sorry.

EDIT: found the code that maybe needs to be fixed

@bddy and @alex-mehdi-dev to take it a step even further, enabling cache and allowing it to run before build automatically (only if needed), you can add the cache options and make it a dependency of build, something like. No need to configure any npm script this way:

"targets": {
	"sync-lock": {
		"executor": "nx:run-commands",
		"options": {
			"command": "npx shx cp *-lock.* {projectRoot}"
		},
		"cache": true,
		"inputs": ["{workspaceRoot}/*-lock.*"],
		"outputs": ["{projectRoot}/*-lock.*"]
	},
	"build": {
		"dependsOn": ["sync-lock"],
	}
}

Additionally I added this line to my .gitignore:

# Or whatever is the path to your project roots.
/apps/*/*-lock.*

@alex-mehdi-dev try removing "externalHelpers": true from your config.

I try it and I still got the problem. In practice it is not that important if I can manage to build production images. I don’t need a lot of speed as it is for development when building them. I will investigate this problem later. I will write here a message if I found something. Thanks for your help !

swc seems not to offer a generatePackageJson property. This feature is quite nice in order to have a package.json file in the dist directory of each project with only the minimum dependencies (smaller Docker image, lower exposure to security vulnerabilities or bugs, etc). Then with this small package.json file we can generate a node_module directory in the dist directory of a project when building the Docker image and copy it.

My current choice is using both swc and webpack.

  • swc for development: very fast, an it is easy to set breakpoints (No specific node_modules in the dist directory of the project but not needed). It is also used of any library.

  • webpack when I need to generate a small docker image for production (generatePackageJson -> package.json with specific dependencies -> generation of specific node_modules -> copy them when building the Docker image.

You can use swc as a compiler with webpack as bunder: https://nx.dev/nx-api/webpack/executors/webpack#compiler

Same here but for ‘pnpm-lock.yaml’.