Since it's node, gatsby-node.js
uses commonjs, components use ES6 modules.
Say I want to share a function that calculates the slug of a created page:
/* export ? */ function getSlug(post) {
return `/posts/${post.data}${post.title.toLowerCase()}`
}
/* module.exports = { getSlug }; ? */
Now I want to import this function both in
gatsby-node.js
to use it in the createPage
APIPosts.js
to link to the individual post paths.How would you recommend sharing code & dealing with the different module systems in this case?
From my experience, you can just import node modules in your browser code like normal, unless you are using Node APIs. Are you facing any problem?
// shared.js
function getSlug(post) {
return `/posts/${post.data}${post.title.toLowerCase()}`
}
module.exports = { getSlug }
// Posts.js
import { getSlug } from './path/to/shared.js'
Most helpful comment
From my experience, you can just import node modules in your browser code like normal, unless you are using Node APIs. Are you facing any problem?