[Gatsby.js] How to Fix 'Duplicated Mapping Key at Line' Error
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
Full Error Message
The code was working fine locally, but I encountered an error when building in the production environment.
error "gatsby-plugin-mdx" threw an error while running the onCreateNode lifecycle:
duplicated mapping key at line 7, column -15:
product_number:
I checked the error log carefully, but there was no specific file name indicating the cause of the error.
Solution
In this case, the error occurred because there was an .mdx
file containing duplicate product_number
fields.
By using VS Code's string search to look for product_number
, I found the .mdx
file that had two occurrences of the same key. After removing the duplicate, everything worked fine.
Files that might cause this error include:
- .yml
- .yaml
- .md
- .mdx
- .json
Additionally, in gatsby-node.js
, you can log the file paths during the onCreateNode
process:
exports.onCreateNode = ({ node }) => {
if (node.internal.type === "MarkdownRemark" || node.internal.type === "Mdx") {
console.log("Processing:", node.fileAbsolutePath)
}
}
If I had logged the file paths when creating nodes, I could have resolved the issue without searching manually.
Hope this helps!