remix icon indicating copy to clipboard operation
remix copied to clipboard

[ERROR] Could not resolve "fs/promises"

Open archraziel88 opened this issue 2 years ago • 17 comments

What version of Remix are you using?

Remix - Cloudflare workers @ latest

Steps to Reproduce

Hello, i'm trying to learn react and remix altogheter, I came across an error, trying to pass a multipart form from frontend to backend via form and action. As i try to import unstable_createMemoryUploadHandler it throws an error related to FS/Promises

17 │ var promises = require('fs/promises'); 

The package "fs/promises" wasn't found on the file system but is built into node. 
Are you trying to bundle for node? 
You can use "platform: 'node'" to do that, which will remove this error.


Build failed with 1 error:
node_modules/@remix-run/node/upload/fileUploadHandler.js:17:23: ERROR: Could not resolve "fs/promises"

So basically I don't understand if I'm doing something wrong. As I could understand on cloudflare workers I have to rely on their services, and so no storage and so no fileUpload, i'm trying to use the stream to get the file posted with the request to pass it to supabase via service role key.

Any idea what am I missing?

Expected Behavior

Should create a uploaderHandle to pass to

export const action: ActionFunction = async ({
  request,
}) => {
  const uploadHandler = unstable_createMemoryUploadHandler({
    maxFileSize: 500_000,
  });
  const formData = await unstable_parseMultipartFormData(
    request,
    uploadHandler
  );

  const file = formData.get("avatar");

  // file is a "File" (https://mdn.io/File) polyfilled for node
  // ... etc

Actual Behavior

17 │ var promises = require('fs/promises'); 

The package "fs/promises" wasn't found on the file system but is built into node. 
Are you trying to bundle for node? 
You can use "platform: 'node'" to do that, which will remove this error.


Build failed with 1 error:
node_modules/@remix-run/node/upload/fileUploadHandler.js:17:23: ERROR: Could not resolve "fs/promises"

archraziel88 avatar May 05 '22 14:05 archraziel88

Did you solve it?

topFEdev avatar May 07 '22 05:05 topFEdev

Did you solve it?

nope. tried a lil' more, as i was trying to figure out how this works, ended up trying setting up a edge function (working on supabase) to overcome the limit.

I couldn't figure out how to go on, and reverted back to next at the moment :)

archraziel88 avatar May 07 '22 08:05 archraziel88

Same issue. Cloudflare ~Worker~ Pages + Typescript node 17.8.0 npm 8.5.5

Regarding You can use "platform: 'node'" to do that, which will remove this error., is this an option I could pass through to esbuild?

roblingle avatar May 09 '22 15:05 roblingle

You can't use node APIs with Cloudflare. They don't even have a file system.

kiliman avatar May 09 '22 16:05 kiliman

I had this issue when trying to "re-platform" a default remix app to cloudflare pages in a monorepo created with nx npx create-nx-workspace@latest nx --preset=@nrwl/remix --project=remix-nx. There were no imports of node APIs in the app code.

I think the issue was that the nx template uses the 'default' remix platform @ v1.0.6, and the imports were from 'remix' rather than '@remix-run/*'. Updating all these references resolved the issue for me:

  • import { Link } from 'remix';
  • import { Link } from "@remix-run/react";

Red herring: I noticed along the way npx remix setup cloudflare and ran that, but I don't think that resolved the issue.

ptim avatar May 13 '22 03:05 ptim

We are also encountering this issue.

There is an article that suggests to use @esbuild-plugins/node-modules-polyfill to polyfill fs, but I was unable to make it work and it still fails with Could not resolve "fs/promises".

Anyone who was able to solve this?

mcmxcdev avatar Jun 20 '22 14:06 mcmxcdev

I encountered the same issue trying re-platform from the default remix app to Cloudflare-Pages.

In my case the fs-extra npm package was the culprit. Once the component that used fs-extra was removed, I was able to build the app. This makes sense since Cloudflare (maybe all serverless platforms?) doesn't have a file system.

redspear avatar Jun 21 '22 02:06 redspear

Anyone who was able to solve this?

Works for me with the adjustments I mentioned above @mcmxcdev 👍

ptim avatar Jun 21 '22 03:06 ptim

Works for me with the adjustments I mentioned above @mcmxcdev +1

Thank you Tim, but we already use the new imports rather than the deprecated ones. We also don't use fs-extra, so no clue where to start.

One way to solve this would be to patch away the line failing the CI run with https://www.npmjs.com/package/patch-package I guess.

mcmxcdev avatar Jun 21 '22 16:06 mcmxcdev

It's happening for me as well, but in remix-run/node Any resolution?

Screenshot 2022-09-18 at 23 05 00

sibasishm avatar Sep 18 '22 19:09 sibasishm

In my case, I was getting this error because I was trying to import from the wrong package,

in entry.client.jsx I had

import { RemixBrowser } from "@remix-run/node";

which should have been

import { RemixBrowser } from "@remix-run/react";

I assume this could happen with other incorrect imports as well

matt416 avatar Nov 04 '22 12:11 matt416

same issue here. I'm trying to use defer in cloudflare worker.

lili21 avatar Nov 09 '22 08:11 lili21

For me, the problem was caused by createCookie from @remix-run/node. I can't remember why, but I think I read somewhere that this must not be in a route file. So moving this to another file fixed the issue.

seancwalsh avatar Nov 10 '22 11:11 seancwalsh

Same error here, no idea what the cause is.

tyteen4a03 avatar Nov 12 '22 02:11 tyteen4a03

Same issu for me, in my case this is due to importing anything (redirect, json) from @remix-run/node in a yarn PNP monorepo. I didn't find a way around it but to use new Response (...)

mxp-qk avatar Nov 26 '22 22:11 mxp-qk

I was facing the same issue.

Solution that worked for me : moved createCookie helper to .server file

shubumpc91 avatar Dec 12 '22 16:12 shubumpc91

This should be resolved. Anyone experiencing this should try upgrading to 0.0.0-experimental-2f4891673.

jacob-ebey avatar Jan 12 '23 00:01 jacob-ebey

This should be resolved. Anyone experiencing this should try upgrading to 0.0.0-experimental-2f4891673.

~Hi Jacob, I've tried both 0.0.0-experimental-2f4891673 and 1.12.0 and I'm still running into this issue. I'm following the bare bones blog tutorial setup with serverBuildTarget set to cloudflare-workers in remix.config.js and the only @remix-run/node imports I have are these two in entry.server.tsx~

import type { EntryContext } from "@remix-run/node";
import { Response } from "@remix-run/node";

Edit: I resolved this by changing the imports from @remix-run/node to @remix-run/cloudflare.

BillBrower-Shopify avatar Feb 01 '23 18:02 BillBrower-Shopify

TS2305: Module '"@remix-run/cloudflare"' has no exported member 'Response'. Hi @BillBrower-Shopify did you get this error ?

Coco-rb2 avatar Mar 09 '23 07:03 Coco-rb2

I ended up switching

import { LinksFunction, MetaFunction } from '@remix-run/node'

to

import { LinksFunction, MetaFunction } from '@remix-run/cloudflare'

jgentes avatar Mar 26 '23 02:03 jgentes

I'm having the same problem with node v16.13.0. Had to use remix-run/cloudflare. We definitely need a better solution for this, I lost a long time trying to find better solutions

felpsio avatar Apr 27 '23 04:04 felpsio

There is now a polyfill for fs/promises when building for non-Node platforms, but as others have said, you should be using @remix-run/cloudflare which doesn't depend on Node built-ins and therefore doesn't need any polyfills.

Note that in the upcoming Remix v2 you'll need to explicitly enable any polyfills via the serverNodeBuiltinsPolyfill option.

markdalgleish avatar Aug 21 '23 02:08 markdalgleish