'ERROR path is not defined ReferenceError: path is not defined gatsby-node.js
I am looking to fetch data from Drupal site to Gatsby via JsonApi.
I want render particular content type with all the details and info.
I am able fetch article title from drupal site with JsonApi but now I want render image and info of that article(content type) on the another page. So i used createPage method from gatsby but i am getting the following error :
**ERROR path is not defined ReferenceError: path is not defined - gatsby-node.js:28**
Reference Link here:
My file structure
- gatsby-node.js
exports.createPages = async ({ graphql, actions, getNodesByType }) => {
const { createPage } = actions;
const allArticles = await graphql(`
{
allNodeArticle {
nodes {
title
drupal_internal__nid
path {
alias
}
relationships {
field_image {
uri {
url
}
}
}
}
}
}
`);
allArticles.data.allNodeArticle.nodes.map(async (node) => {
createPage({
path: node.path.alias,
component: path.join(__dirname, '/src/Components/Articledetails.js'),
context: {
id: node.drupal_internal__nid,
}
})
});
}
- listing.js file
import React from "react";
import { graphql, Link } from "gatsby"
const listing = ({data}) => {
return (
data.allNodeArticle.nodes.map((info) =>
<div>
<div>
<h2><Link to={"/node/" + info.drupal_internal__nid}>{info.title}</Link></h2>
</div>
</div>
)
)
}
export const query = graphql`
{
allNodeArticle {
nodes {
title
drupal_internal__nid
path {
alias
}
relationships {
field_image {
uri {
url
}
}
}
}
}
}
`
export default listing
Solution 1:[1]
const path = require('path')
import the path module , it will work
Solution 2:[2]
Outside the scope of the question, you should replace your map loop for a forEach loop:
allArticles.data.allNodeArticle.nodes.forEach((node) => {
createPage({
path: node?.path?.alias,
component: path.join(__dirname, '/src/Components/Articledetails.js'),
context: {
id: node.drupal_internal__nid,
}
})
});
Double-check that path and alias are fulfilled in all article references.
Regarding your main issue (ERROR path is not defined ReferenceError: path is not defined - gatsby-node.js:28), is because, to use path, you need first to import it as a module:
const path = require(`path`);
This will allow you to do path.join.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Himanshu Rathore |
| Solution 2 |

