4

I have deployed my Next.js project to the Azure App service that needs to include "node_modules" to run the production ("npm start").

But.. I found that the "node_modules" size is so big (354,3 MB on disk).

And then I saw that we could use the "Output File Tracing" feature to reduce the size of deployments drastically.

So how can I use that feature? I think that will affect my Azure Pipeline and Release

1 Answer 1

6

Below are the steps of solution how I deploy my Next.js project to Azure App Service with the "Output File Tracing" feature.

Project Setup

1. Your Next.js version should be version 12.2.x or higher. So upgrade your Next.js first to the required version.

2. Add output: "standalone in 📄 next-config.js

// example
const nextConfig = {
  output: "standalone",
};

module.exports = nextConfig;

3. And you can try to build it. npm run build

4. There will be a folder called 📁 standalone inside 📁.next

5. Inside the 📁 standalone, there is a 📄 server.js.

6. Open your cmd/terminal and then change the directory to 📁 standalone with cd .next/standalone. You can run the 📄 server.js using node. Example: node server.js. And then it will be run in port 3000. You can open it in the browser and then access localhost:3000.

7. There will be some errors of "some files are missing" (404). You need to copy the 📁 static inside 📁 .next to /.next/standalone/.next/ and re-run the node server.js.

7.1. You can update your script to copy it automatically when you run the build script. First install cpy-cli as dev-dependencies npm i cpy-cli -D.

7.2. In 📄 package.json. Update build script

"build": "next build && ./node_modules/.bin/cpy '.next/static/**' '.next/standalone/.next/static/'"

7.3. Try to build again and then the 📁 static will be copied automatically.

Pipelines

Archive only 📁 standalone.

azure-pipelines.yml

# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- task: NodeTool@0
  inputs:
    versionSpec: '18.x'
  displayName: 'Install Node.js'

- script: |
    npm install
    npm run build
  displayName: 'npm install and build'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: "./.next/standalone/"
    includeRootFolder: false
    archiveType: "zip"
    archiveFile: "$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip"
    replaceExistingArchive: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: "$(Build.ArtifactStagingDirectory)"
    ArtifactName: "drop"
    publishLocation: "Container"

Release

enter image description here

Done 🌟

If you facing a problem like the public content is missing, you need to copy your 📁 public to 📁 standalone. You can do it manually or update your build script to be like this:

package.json

"build": "next build && ./node_modules/.bin/cpy '.next/static/**' '.next/standalone/.next/static/' &&  ./node_modules/.bin/cpy './public/**' '.next/standalone/public/'",
Sign up to request clarification or add additional context in comments.

6 Comments

I follow this way. It works properly, but problem is next image optimization not working. Any solution please.
@data utama, How can I properly add image optimization in your standalone process.
I only add sharp as built-in image optimization in my project nextjs.org/docs/messages/install-sharp
I am getting this error in linux - Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly . I also add this in the .env - NEXT_SHARP_PATH=/tmp/node_modules/sharp. Any solutions or idea about it.
You can check this link nextjs.org/docs/messages/sharp-missing-in-production to fix the sharp error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.