hozana / next-translate-routes

Flexible and translated routes for Next.js without custom server
MIT License
115 stars 31 forks source link

Module not found: Can't resolve 'next/dist/shared/lib/router-context' #88

Open robatronPrime opened 8 months ago

robatronPrime commented 8 months ago

In next@14.1.0 the router-context file has changed name to router-context.shared-runtime.

Changing the line at ./node_modules/next-translate-routes/react/withTranslateRoutes.js:38:0 from var router_context_1 = require("next/dist/shared/lib/router-context"); to var router_context_1 = require("next/dist/shared/lib/router-context.shared-runtime"); fixes the issue.

However, it would be cool if it new which version of next was being used and changed the import depending on the next version?

artkondakov commented 7 months ago

Got the same problem and yes, @robatronPrime solution is really working. I tried to use Webpack config to add alias

"next/dist/shared/lib/router-context$": path.join(
    __dirname,
    "..",
    "..",
    "node_modules",
    "next/dist/shared/lib/router-context.shared-runtime.js"
),

but it doesn't work...

patrykmostowski commented 7 months ago

Provided a fix, your next version was set to latest, when checking the next version the plugin checked for a number, and omitted 'latest' string, setting ur Router Context path to old, resulting in this problem

If you need a quick solution for a cloud deployment like heroku or netlify just use yarn/pnpm/npm install next@14.1, fixes the issue

dsmiletzki commented 7 months ago

In next@14.1.0 the router-context file has changed name to router-context.shared-runtime.

Changing the line at ./node_modules/next-translate-routes/react/withTranslateRoutes.js:38:0 from var router_context_1 = require("next/dist/shared/lib/router-context"); to var router_context_1 = require("next/dist/shared/lib/router-context.shared-runtime"); fixes the issue.

However, it would be cool if it new which version of next was being used and changed the import depending on the next version?

Same problem for me.

artkondakov commented 7 months ago

Maybe anyone had successfully solved the problem for the next-translate-routes v10 with aliases?... I just can't understand why my solution isn't working

patrykmostowski commented 7 months ago

i have written a temporary solution, especialy if you need a quick fix for a cloud deployment just add this script in root directory.

const fs = require('fs');
const path = require('path');

/**
 * @param {string} filePath - The path to the file.
 * @param {string} searchPattern - The RegExp pattern to search for.
 * @param {string} replaceString - The string to replace with.
 */
function replaceLineInFile(filePath, searchPattern, replaceString) {
  const fileContent = fs.readFileSync(filePath, 'utf8');

  const modifiedContent = fileContent.replace(searchPattern, replaceString);

  fs.writeFileSync(filePath, modifiedContent);
  console.log(`File updated: ${filePath}`);
}

const filePath = path.join(__dirname, 'node_modules', '/next-translate-routes/react/', 'withTranslateRoutes.js');
const searchPattern = /var router_context_1 = require\("next\/dist\/shared\/lib\/router-context"\);/g;
const replaceString = 'var router_context_1 = require("next/dist/shared/lib/router-context.shared-runtime");';
replaceLineInFile(filePath, searchPattern, replaceString);

from there, you just run it before build with node, and then everything should work as expected

patrykmostowski commented 7 months ago

Bumping the thread. Would be nice if someone addressed this issue and went through PR's waiting to be merged. 👍🏻

redveronika commented 7 months ago

Have the same issue, bumping the thread.

cvolant commented 6 months ago

Hi! Sorry that it took so long, but I went through 3 intense months, and had to prioritize. I just release 1.11.0-5, that basically drop support for old next version (<13.5.0), in order to fix this issue. I tried hard to maintain the support, as reflected in the version number, without success. I hope it is ok this time.