Skip to content

[Bug]: publishDir incorrect when deploying using Netlify CLI with cwd option #1258

Closed
@lourd

Description

@lourd

Summary

My team has an Nx-managed monorepo. We recently started deploying our web app to Netlify using the CLI. Up till now we've gotten by with having our netlify.toml in the root of our repo, but we're trying to colocate that config with the app in order to allow for multiple web apps to be deployed from the monorepo. In pursuit of that, I've been trying to use the lesser-known cwd option that the CLI provides.

Long-story short, using the cwd option does not work with the Next.js build plugin.

There's two problems. The first appears to be with the Netlify CLI not passing the cwd option along, which affected how the publish directory was set. I debugged that and made a patch, see this commit.

Fixing that allowed the build and deploy to finish. But then the deploy preview has an error on any page that hits one of the back-end functions about not being able to find required modules. For example, see this link https://622969f742f98859a319f7ab--suspicious-shannon-5bd1ac.netlify.app/fooooooo.

Steps to reproduce

  1. Generate an Nx workspace
  2. Generate a Next.js app in the workspace
  3. Add a netlify.toml file to the app's directory
  4. Try to build & deploy using the netlify CLI and the cwd flag. See here in the reproduction repo for example.

This fails:

Error: The directory "../.." does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
  1. After applying this patch, try building and deploying again. This should succeed.
  2. On the deployed app, do something that will require one of the back-end functions, like going to a route that doesn't exist. Netlify returns a 502 error with this content:
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '../../../Users/louis/code/netlify-cli-repro/dist/apps/toy-app/.next/required-server-files.json'\nRequire stack:\n- /var/task/apps/toy-app/.netlify/functions-internal/___netlify-handler/___netlify-handler.js\n- /var/task/___netlify-handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module '../../../Users/louis/code/netlify-cli-repro/dist/apps/toy-app/.next/required-server-files.json'",
    "Require stack:",
    "- /var/task/apps/toy-app/.netlify/functions-internal/___netlify-handler/___netlify-handler.js",
    "- /var/task/___netlify-handler.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:202:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    "    at Module.load (internal/modules/cjs/loader.js:950:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

A link to a reproduction repository

https://github.com/lourd/netlify-cli-repro

Plugin version

4.2.7

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
[build]
  command = "yarn build toy-app"
  publish = "../../dist/apps/toy-app/.next"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
// eslint-disable-next-line @typescript-eslint/no-var-requires
const withNx = require('@nrwl/next/plugins/with-nx');

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
};

module.exports = withNx(nextConfig);

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugcode to address defects in shipped code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions