英文:
Getting error when trying to use a package that imports JSON file for i18n
问题
I have a package that I'm importing into my Next.js project (v13.4.3) that has a sub-dependency on another package which supplies it with translations via a JSON file. However, when I attempt to use the package and compile, I get this error:
- info Collecting page data ..Error: Cannot find module '/Users/user/app/.next/server/chunks/translations.json'
at webpackEmptyContext (/Users/user/app/.next/server/chunks/7208.js:9:10)
at Object.9641 (/Users/user/app/.next/server/chunks/9716.js:10551:45)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.11450 (/Users/user/app/.next/server/chunks/9716.js:5138:31)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.4847 (/Users/user/app/.next/server/chunks/9716.js:5309:14)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.59204 (/Users/user/app/.next/server/chunks/9716.js:6898:14)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at /Users/user/app/.next/server/pages/projects/[id].js:27:79 {
code: 'MODULE_NOT_FOUND'
}
When I go into the actual node_module folder, the .json file is indeed there. Might be worth noting that both packages belong to an NPM scope (e.g. @mycompany
), which was added to transpilePackages
array as ["@mycompany"]
to prevent a separate error I was getting that said SyntaxError: Unexpected token 'export'
. I also tried using Remix.run to see if it would work there, but I ended up getting an identical error message.
How should I go about fixing this?
英文:
I have a package that I'm importing into my Next.js project (v13.4.3) that has a sub-dependency on another package which supplies it with translations via a JSON file. However, when I attempt to use the package and compile, I get this error:
- info Collecting page data ..Error: Cannot find module '/Users/user/app/.next/server/chunks/translations.json'
at webpackEmptyContext (/Users/user/app/.next/server/chunks/7208.js:9:10)
at Object.9641 (/Users/user/app/.next/server/chunks/9716.js:10551:45)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.11450 (/Users/user/app/.next/server/chunks/9716.js:5138:31)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.4847 (/Users/user/app/.next/server/chunks/9716.js:5309:14)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at Object.59204 (/Users/user/app/.next/server/chunks/9716.js:6898:14)
at __webpack_require__ (/Users/user/app/.next/server/webpack-runtime.js:25:42)
at /Users/user/app/.next/server/pages/projects/[id].js:27:79 {
code: 'MODULE_NOT_FOUND'
}
When I go into the actual node_module folder, the .json file is indeed there. Might be worth noting that both packages belong to an NPM scope (e.g. @mycompany
), which was added to transpilePackages
array as ["@mycompany"]
to prevent a separate error I was getting that said SyntaxError: Unexpected token 'export'
. I also tried using Remix.run to see if it would work there, but I ended up getting an identical error message.
How should I go about fixing this?
答案1
得分: 1
SASS正在被Vercel使用,而Next.js具有内置对Sass的支持,可以使用.scss
和.sass
扩展名。
你在Next.js中遇到的错误可能与SASS错误有关,正如vercel/next.js
问题11377所示。在某些情况下,当发生SASS错误时,Next CLI将继续打印“Cannot find module
”错误,直到SASS错误被修复为止。
一个解决方法是在.next
目录中手动创建一个build-manifest.json
文件,内容为{}
。这似乎可以暂时解决问题,并允许其他更明智的错误出现。
另一位用户报告通过运行yarn upgrade
命令来解决此问题。
如果上述解决方法不起作用,你可以尝试以下步骤,如“每当我尝试运行'npm run dev'时都会出现'ERROR MODULE NOT FOUND'”中建议的:
- 停止你的开发服务器(ctrl+c)。
- 删除项目根目录中的
.next
文件夹。 - 同样删除
node_modules
文件夹和package-lock.json
文件。 - 运行
npm cache clean --force
。 - 运行
npm install
。 - 运行
npm run dev
。
如果即使在按照上述步骤之后问题仍然存在,检查指定的文件是否存在于指定的路径上。如果存在,请尝试将项目文件夹移到除了desktop
之外的路径,因为可能存在Windows权限问题,但我怀疑这不应该是问题的原因(/Users/...
:MacOS?)。
在对项目的文件或目录进行更改之前,请始终备份您的工作,以防止任何数据丢失。
英文:
SASS is being used by Vercel, and Next.js has built-in support for Sass using both the .scss
and .sass
extensions.
The error you are experiencing with Next.js might be related to a SASS error, as illustrated by vercel/next.js
issue 11377. In some cases, when a SASS error occurs, the Next CLI will continue to print a "Cannot find module
" error until the SASS error is fixed.
One workaround for this issue is to manually create a build-manifest.json
file with the content {}
in your .next
directory. This seems to temporarily fix the issue and allows other more sensible errors to appear.
Another user reported fixing the issue by upgrading yarn using the command yarn upgrade
.
If the above solutions do not work, you might try the following steps as suggested in "ERROR MODULE NOT FOUND
whenever I try to run 'npm run dev
' using Next.js":
- Stop your development server (<kbd>ctrl</kbd>+<kbd>c</kbd>).
- Delete the
.next
folder in your project's root folder. - Delete the
node_modules
folder and thepackage-lock.json
file as well. - Run
npm cache clean --force
. - Run
npm install
. - Run
npm run dev
.
If the issue persists even after following the steps above, check if the specified file exists on the specified path. If it does, try moving your project's folder to a path other than desktop
due to potential Windows permission problems, but I suspect this should not be the case here (/Users/...
: MacOS?).
Remember to always back up your work before making changes to your project's files or directories, to prevent any data loss.
答案2
得分: -1
如果您在尝试使用导入JSON文件进行国际化(i18n)的包时遇到错误,有一些潜在的原因和解决方法可以尝试:
-
验证文件路径:仔细检查JSON文件的文件路径是否正确。确保文件位于指定位置,并且包代码中提供的路径与实际文件路径相匹配。
-
检查文件权限:确保JSON文件具有包访问所需的读取权限。确保文件没有被锁定或受到任何阻止读取的限制。
-
确认JSON文件的有效性:确保JSON文件本身是有效且格式良好的。JSON语法错误可能导致导入或解析文件时出现问题。您可以使用在线的JSON验证工具来检查文件的有效性并修复任何语法错误。
-
检查缺少的依赖项:某些包需要特定的依赖项来处理JSON文件的导入。验证是否已安装了包所需的所有必要依赖项。您可以查阅包的文档或README文件以确定需要安装的任何依赖项。
-
更新包:确保您正在使用包的最新版本。有时,通过将包更新到最新版本可以解决兼容性问题或错误。检查包的官方网站或包管理器(例如npm、pip)以查看是否有更新或错误修复。
-
寻求来自包社区的支持:如果您尝试了上述步骤仍然遇到问题,考虑与包社区寻求支持。这可以包括与包相关的论坛、GitHub存储库或讨论板。详细描述您的问题并提供任何相关的错误消息,以获得其他用户或包维护人员的帮助。
英文:
If you are encountering an error when trying to use a package that imports a JSON file for internationalization (i18n), there are a few potential causes and solutions you can try:
Verify the file path: Double-check that the file path to the JSON file is correct. Ensure that the file is located in the specified location and that the path provided in the package code matches the actual file path.
Check file permissions: Ensure that the JSON file has the necessary read permissions for the package to access it. Make sure the file is not locked or restricted in any way that would prevent it from being read.
Confirm JSON file validity: Ensure that the JSON file itself is valid and well-formed. JSON syntax errors can cause issues when importing or parsing the file. You can use online JSON validators or tools to check the validity of the file and fix any syntax errors.
Check for missing dependencies: Some packages require specific dependencies to handle JSON file imports. Verify that you have installed all the necessary dependencies required by the package. You can consult the package's documentation or README file to identify any dependencies that need to be installed.
Update the package: Ensure that you are using the latest version of the package. Sometimes, compatibility issues or bugs can be resolved by updating the package to the most recent version. Check the package's official website or the package manager (e.g., npm, pip) for any updates or bug fixes.
Seek support from the package's community: If you have tried the above steps and are still experiencing issues, consider reaching out to the package's community for support. This can include forums, GitHub repositories, or discussion boards related to the package. Describe your problem in detail and provide any relevant error messages to receive assistance from other users or the package maintainers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论