Failed to load .env file ".env": Error: ENOENT: no such file or directory
Hey, Im new to using cloudflare and new to using Hono. I now came across an error that tells me Im missing a .env file, which also should contain some variables. The problem is, idk where I use these and what their values should be. It seems like these should be using system variables instead (Im currently on Windows 10), but I could find an answer online. This only started appearing after I added and tried using Hono.
Here is my code:
The wrangler.toml file:
62 Replies
The console output:
And the logs:
The goal of this worker would be to serve an rss feed
and when I add
node_compat = true
to the wrangler.toml file, this is the new output:
Console:
Logs
On cloudflare workers you want to use .dev.vars for environment variables
You can use the dotenv package you can access the variables inside context only on c.env
GitHub
GitHub - NicoPlyley/hono-auth: An example app with authentication u...
An example app with authentication using Hono, Drizzle, and D1. Running on CF Workers - NicoPlyley/hono-auth
Here is an example cf workers api
thanks for the response.
My problem is not that I want to use environment variables in my code.
I have the problem that I cant build/ test my worker locally after I tried using Hono. The error says it is something with environment variables, but I don't use environment variables in my code.
My problem is that I don't understand how to get rid of this error so that it builds/ works locally locally again. I want to fix this bug/ error, not implement environment variables.
Can you show me your package.json
Here you go:
So your dependency for dotenv is likely causing the issue. Is not compatible with cf workers
It attempts to use file system which is a node specific library that is not supported
I see
if I remove it, it still doesn't work though
Possible the @hono/node-server is also not working
Make sure you do a bun/npm remove package-name and not just delete those lines of code
oh ya, someone told me to adding that to fix that bug, idk, I shouldn't need it
Only if your running the application with node you would need that
I removed the dotenv and node-server with npm and it still fails
Logs:
It still stays dotenv so I wonder if that discord package is using it
maybe
Did you remove the dotenv on line 3 on index.ts?
there never was no dotenv, idk what you mean
That’s weird, remove your .wrangler folder and reinstall wrangler
It has a glitch on it somewhere. Does anything in createRssFeed.ts use an external package?
Or better yet I’d delete the node modules and do a fresh install of all packages
ok, I will try that
Also you don’t have a file on your dev and start commands so
wrangler dev src/index.ts
Also you can update your deploy as well
wrangler deploy --minify src/index.ts
I removed the wrangler config and the node modules folder, after that I ran these commands:
npm install wrangler --save-dev
npm i feed
npm i discord.js-selfbot-v13
npm i hono
it also doesnt seem like the wrangler config got generated again, I assume thats one part of the problemSome package must not be compatible it’s so weird it says in your index file
It just be from the import of the rss file something in there is causing it but it’s not mapping right
now, idk why, feed is not recognized
Can you comment out everything on rss. Return just a string and then we can verify it’s coming from a package
So don’t import anything on your index
🥳
do I have to initialize wrangler with
npx wrangler init
again here?I don’t believe you do
That’s only for setting up a project with a framework I believe
So something in feed has an issue I would try and find an alternative that works on edge platforms
yes, thats what I assumed, but the wrangler config is not there now
yes, I will look into that. Thanks a lot for the help. What exactly do you mean with "that works on edge platforms" though?
Most edge/serverless platforms don’t use node. They just use the chrome v8 engine. So if a package has node libraries it won’t work
Cloudflare Docs
Node.js compatibility · Cloudflare Workers docs
Implemented Node.js runtime APIs and enablement instructions for your Worker project.
So these are the only ones compatible with cf workers
npm
rss-parser
A lightweight RSS parser, for Node and the browser. Latest version: 3.13.0, last published: a year ago. Start using rss-parser in your project by running
npm i rss-parser
. There are 325 other projects in the npm registry using rss-parser.This appears to work on cf wokers. Since it can run in web it should have any dependencies that require node
I see, thats why it told me to add
node_compat = true
to the configActually you can use your rss one. I didn’t not realize String Decoder was available on cf workers
So add this to your wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
I dont have a wrangler.toml. also, thats what I had last time too
Just create on in your root folder
GitHub
starter/templates/cloudflare-workers/wrangler.toml at main · honojs...
Hono starter templates. Contribute to honojs/starter development by creating an account on GitHub.
Copy this one. Just add project name
done, I still have an issue with importing feed and idk why
What does the issue say
Cannot find module 'feed' or its corresponding type declarations
Might be an error with the code editor
Sometimes they don’t update
So no guarantee this will make it work because it might still be trying to use dotenv but we’ll see
ok, so should I try restarting the local dev instance with the rssFeed code included again?
Did you restart the reload the wrangler.toml file?
how do I do that?
Just stop wrangler and start it again
The toml file is not hot reloaded
how do I stop it? isnt that done by using
x
to close the local dev environment and start it again with npx wrangler dev src/index.ts
?
cause that is what I did
else, I dont know how to stop wranglerYup that would be right
well, then these are the logs I get from that
btw, after restarting the ide, the import error went awai, so thats fine
So it’s working now?
no
it fails to build
it seems something uses string_decoder which is part of that nodejs server
I’m not sure why it says it’s available
does that mean the solution would be to find another module with which I can create an atom feed, that doesn't use nodejs server modules? How can I tell if a module is doing that?
Yes that would be best. I sent one earlier that appears to work on cf workers
This one here
If they advertise it works on a browser then it should all work in cf
Alright, I will give it a shot, thanks a lot
Hey, this is a package to parse an rss feed, but I want to create an rss feed and serve it with my worker. Is it correct that I only have to look out for it being compatible with the chrome v8 engine? This linked rss parser for example, also lists nodejs, so I am confused about how I can tell if a package is compatible or not. Thanks a lot for the help and sorry for the beginner questions
It’s no worries. Most packages don’t advertise it being available for v8. But if they are browser compatible or you use import them using a script tag then they should work on cf workers
they would have to tell you that they are browser compatible for me to know, or is there another way to tell besides trying it out?
or you use import them using a script tag then they should work on cf workersdo you mean that I can also use nodejs packages if I
import themwith a script tag
? I dont know how I would do that in workers though, as that sounds like html, which would be cf pages if I understand this correctlyI’m just meant if they have an option to use a script tag then they don’t use node libraries
Honestly I don’t know if a great way to check there might be a website out there. But I’ll look later today see if I find anything
amazing, thanks a lot for the help. I will also keep an eye open