'NextJS next.config.js symlink take wrong path

For my last project, I'm facing some issue on the production server. Everything works fine on my dev environment.

I have a NextJS 12.0.7 project working with Node 16.14.2. I use Capistrano to send my project to production server.

For some reason, I need to have different next.config.js for the same project. So I use the :linked_file property from Capistrano in order to put my next.config.js in the shared folder and create a symlink to my project root folder.

It looks like this :

.env -> /var/www/project/front/shared/.env
.env.example
.gitignore
.npmrc -> /var/www/project/front/shared/.npmrc
Capfile
Gemfile
bitbucket-pipelines.yml
capistrano
ecosystem.json -> /var/www/project/front/shared/ecosystem.json
i18n.json
locales
next.config.js -> /var/www/project/front/shared/next.config.js
node_modules
package-lock.json
package.json
pages
public
styles
templates
utils

The problem appears when I run npm run build. It shows me that package used in the next.config.js does not exist :

> Build error occurred
Error: Cannot find module 'next-translate'
Require stack:
- /var/www/project/front/shared/next.config.js

The problem is because the require('next-translate') start from the symlink path and not the project root path. It works on an other server with barely the same configuration (Node 14). I did nothing else on this server to make it works. I've tried Node 14 and it's the same issue.

It looks like a relative path handling because when I put the next.config.js directly in the root folder (instead of the symlink), it works.

I can give more informations on demand.

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source