Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am using [email protected] (Pages router) and when I compile appear this error "Can't resolve 'next/dist/shared/lib/router-context'" #94

Open
Jacky16 opened this issue Jun 13, 2024 · 6 comments

Comments

@Jacky16
Copy link

Jacky16 commented Jun 13, 2024

I put this in webpack config on next.config

 webpack: (config) => {
    config.resolve.alias["next/dist/shared/lib/router-context$"] = path.join(
      __dirname,
      "node_modules",
      "next/dist/shared/lib/router-context.shared-runtime.js"
    );


    return config;
  },

The error in terminal:

Screenshot 2024-06-13 at 09 11 34

@Jacky16 Jacky16 changed the title I am using NextJS 14 (Pages router) and when I compile appear this error "Can't resolve 'next/dist/shared/lib/router-context'" I am using [email protected] (Pages router) and when I compile appear this error "Can't resolve 'next/dist/shared/lib/router-context'" Jun 13, 2024
@pug-pelle-p
Copy link

Hi! I get the same error as you and the code you used above isn't working. Do you have any tips on how to make this work?

@pug-pelle-p
Copy link

pug-pelle-p commented Aug 27, 2024

My solution to this was to add a new file build-fix.js with the following code (as suggested here: ):

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);

And update package.json with the following:

...
  "scripts": {
    "dev": "node build-fix.js & next dev",
    "build": "node build-fix.js & next build",
    "start": "node build-fix.js & next start",
    "lint": "node build-fix.js & next lint"
  },
  ...

@jaumamyfront-ender
Copy link

I put this in webpack config on next.config

 webpack: (config) => {
    config.resolve.alias["next/dist/shared/lib/router-context$"] = path.join(
      __dirname,
      "node_modules",
      "next/dist/shared/lib/router-context.shared-runtime.js"
    );


    return config;
  },

The error in terminal:

Screenshot 2024-06-13 at 09 11 34

i solved this problem #97

@0xyy
Copy link

0xyy commented Dec 4, 2024

@pug-pelle-p what version of next and next-translate-routes are you using with that working fix ?

@0xyy
Copy link

0xyy commented Dec 4, 2024

@jaumamyfront-ender I tried this solution but didn't work, what versions of next and next-translate-routes are you using ?

@jaumamyfront-ender
Copy link

jaumamyfront-ender commented Dec 23, 2024

@0xyy 14.1.4 version of my next.js)if you have problm you have many solutions,
1-just download this my package fix from npm(firstlydelete original)https://www.npmjs.com/package/next-translate-route
or-
2-read carrefully, this my solution with use package fix #97
3.find somethong else ;)
good luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants