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
gatsby-node.js doesn't allow ES6 import #7810
Comments
That file is run by node.js so it supports whatever the version of node you're using supports. Es6 module support in node is still a wip https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71 |
I'm also running into this. I need to use |
What would be the steps to enable |
Is there a way to use |
I use esm for this and it works so far. Here's what I did:
@KyleAMathews Is there anything dangerous about doing it this way? Because if it's safe I could add it to the docs :) |
This pattern definitely works @nikoladev! Not sure where it'd go in the docs though. Any ideas @gatsbyjs/docs? |
Here's how I did it:
It seems to work well enough but I wonder if it might not mess with the cache? I think the cache is supposed to reset when you modify |
@KyleAMathews How about under Sourcing Content and Data? At least that's the reason I used it for. |
It's kind of a different debate, but any chance using |
@KyleAMathews, is there a way to require a local ES6 file? For example, I have a // src/utils/article.js
import { format } from 'date-fns'
export const createArticleUrl = (a) => (
`/${format(a.publishDate, 'YYYY')}` +
`/${format(a.publishDate, 'MM')}` +
`/${format(a.publishDate, 'DD')}` +
`/${a.category.urlSlug}` +
`/${a.urlSlug}`
) and the my // gatsby-node.js
...
const { createArticleUrl } = require(`./src/utils/article`)
... And I get the error:
Any ideas? I use the function to create the url for our articles/posts and would like to import it like I do in my React components. Thanks! |
I was able to resolve my issue by using ES5 in my // src/utils/article.js
const { format } = require('date-fns')
var createArticleUrl = function (a) {
return (
`/${format(a.publishDate, 'YYYY')}` +
`/${format(a.publishDate, 'MM')}` +
`/${format(a.publishDate, 'DD')}` +
`/${a.category.urlSlug}` +
`/${a.urlSlug}`
)
}
module.exports.createArticleUrl = createArticleUrl and then // gatsby-node.js
...
const { createArticleUrl } = require(`./src/utils/article`)
... I can also import |
Is the 5th "Use |
@WeZZard You're totally right! I'll fix it in my post. |
Another way I found this to work was by updated your package.json with the following:
No need to create new files |
@reaktivo your scripts work locally but I wasn't able to get it to work with netlify. Are u able to deploy your site to netlify with |
@rotexhawk Just pushed an example and it worked fine: https://github.com/reaktivo/gatsby-esm/ Make sure you're running Check out this commit: reaktivo/gatsby-esm@cf62025 |
Thanks, I have installed esm. the problem is that netlify doesn't have access to |
@rotexhawk That's really weird, considering the project that I sent you was deployed to Netlify... anyway, if you need to avoid "scripts": {
"build": "node -r esm ./node_modules/bin/gatsby build",
"develop": "node -r esm ./node_modules/bin/gatsby develop",
"start": "npm run develop",
"serve": "node -r esm ./node_modules/bin/gatsby serve",
"test": "echo \"Write tests! -> https://gatsby.app/unit-testing\""
}, |
@rotexhawk Got in touch with the Netlify team, you might also need to specify the NPM_VERSION, see https://www.netlify.com/docs/build-settings/#node-npm-and-yarn |
@reaktivo will do. thanks for all the help. |
I would strongly consider adding that esm approach to documentation, since Gatsby is indicating es6 modules should work inside gatsby-node.js file. This error message is printed, when you mix modules:
It clearly indicates, you CAN use es6 modules. |
Gatsby no permite incluir importaciones en su fichero Node. Esta solucíón está sacada de un hilo de Github gatsbyjs/gatsby#7810 (comment), donde el paquete incluido resuelve el problema permitiendo realizar importaciones.
|
Actually it seems like it only works for the first level import in For example, ./gatsby-node.ems.js
./foo
Throws an error:
|
Since Node modules are almost there, I wanted to use node with I changed scripts.start to
|
@wesbos @reaktivo are you able to use that fix still with the latest gatsby (2.22.17 in my case)? I've had it working without any issues using that solution, but updated gatsby today and started getting the import errors again:
|
As an alternative, you may want to use TypeScript for |
my esm trick above just stopped working. I even rolled back gatsby and node version and it persists. Detailed it here if anyone has the same issue: reaktivo/gatsby-esm#1 @caycecollins did you find a fix? |
Just a heads up that I struggled with this for an hour and finally figured out that no matter what I put in my package.json for build, the Netlify UI configuration was taking precedence. It wasn't until I added a netlify.toml that this became apparent. Moral of the story, edit your Netlify UI build settings, or clear them out and toss it in the config. I prefer the latter. 😄
It's worth noting that yes, this worked for me:
|
Wow, I understand change is hard, but ES Modules are Javascript now, so it's really disappointing to see (for whatever totally valid reasons) the maintainers fighting against supporting Javascript ... especially when JDalton and his |
P.S. A few relevant details that you might not be aware of ...
So it's "gain modern Javascript language features with three lines of code" or ... fight it for ? benefit. If there's interest, I'd be more than happy to submit a PR :) |
Can someone provide a final solution and lock this thread? Hate doing this dance of skimming all comments in really important but seemingly closed issues where conversation is still happening and trying to sift through all comments to see which one has the most thumbs ups and is likely to be the best solution 😔 |
Like I said, the hard part is not making the The hard part is just getting a maintainer with that knowledge to care :( You'd think that even if every maintainer had a personal hated for modern JS module syntax, they still could at least appreciate their users' desire for it ... but the refusal to even re-open this issue, let alone fix it (again, possibly with only two lines of code) suggests otherwise. Given how great this team has been with other issues, it's honestly confusing to me. |
Any word from maintainers on this subject, please? |
Just got bitten by this but in Whatever is your ideology, I think we can agree consistency is always a good practice. And here the import format is inconsistent within gatsby. |
i also think this is kind of important enough to be revisited. |
It's not worth implementing the esm module into Gatsby - that is a great solution for now, but the future is using native esm in Node by either:
We will have some growing pains as node moves to esm and we wait for lots of packages to catch up, but as long as authors know that as well at the node version limitations, we should be able to at least use |
The |
Then use "type": "module" in your package.json and use .cjs for any legacy stuff. There are only two options - gatsby isn't going to force you to do anything - this is a standard in Node.js |
Thanks :) |
mjs is going to be needed in some cases where you have a massive commonjs project. You can't just snap your fingers and have the thing swich over. you need to do it bit by bit, and opting into esm on a per file basis is a good solution until you can get there. I realize it's an ugly extension (?) and it's funny to say michael jackson script, but that is a good option going forward |
Sure, but asking a fully working ESM-using app to rename all its files just to use modern JS syntax is complete idiocy. Again, the Node org provided options for a reason, and my only concern was making sure Gatsby supports those options (or better yet, supports the
(which clearly implies not supporting the other option) |
Man, what a welcoming community Gatsby has. A user asks for something basic (like support for the JS language) and this is the response they get. |
this also worked for me! make sure to install the package first: package.json
gatsby-config
gatsby-node
|
oops @alishaevn did you mean |
yes @01binary that is what I meant! updated. 🙂 |
Please go to #34613 and you can use TypeScript with Gatsby. By the time you're reading this it's also possible that the feature already was shipped in Gatsby 4. If that is the case the RFC will be marked as done. You can also expect ESM support (through this) very soon, please follow #31599 for progress. Again, if the feature is shipped it'll be noted in the discussion. |
Description
gatsby-node.js
doesn't allow ES6 javascript.Steps to reproduce
gatsby-node.js
:Expected result
gatsby-node.js
should be transpiled and allow ES6 likegatsby-ssr.js
orgatsby-browser.js
.Actual result
Error
Environment
The text was updated successfully, but these errors were encountered: