Can't resolve 'fs' in cloudflare workers

I’m developing a worker in which I need to use node js fs module.
But I’m getting error like below:
Module not found: Error: Can't resolve 'fs' i

Can anyone help me who has faced this issue or am I doing something wrong ? Please correct me.

Thanks

A worker environment is not Node.js, so you cant use these modules. Also, you wouldnt have access to the file system anyhow.

Is there a way to use node as an environment or maybe some other way to access directory?

There is not.

1 Like

@patel.gopal55555 If you look at the Cloudflare Worker Template library for Apollo GraphQL you will see you can use an alias in Wrangler webpack config to replace the ‘fs’ (filesystem) module with a null module to exclude it and allow the code to compile.

It depends upon the npm library you are trying to import, wether at runtime the code you’re using actually uses the ‘fs’ module. In the case of the npm Apollo GraphQL package it is not required for the majority of the functionality.

You can use this approach to sub out other npm dependancies.

2 Likes