assert is not a function error, when import csrf in middleware.js
Answered
andresdotsh posted this in #help-forum
Hi everyone 👋,
I'm having the following problem and I was wondering if you could help or give me an idea of what's going on.
You can try this on a fresh Next.js installation. [code example repo](https://github.com/andresdotsh/csrf-next-issue/tree/main) 👈
When I import the
This is because the [csrf](https://www.npmjs.com/package/csrf) library depends on the [rndm](https://www.npmjs.com/package/rndm) library, which uses NodeJS's
This error only occurs if I import it in the middleware.js file, it doesn't happen in page.js for example.
Does anyone have any idea why this might be happening? I would like to use this
More info:
-
-
-
-
- I'm using nextjs
Pictures:


I'm having the following problem and I was wondering if you could help or give me an idea of what's going on.
You can try this on a fresh Next.js installation. [code example repo](https://github.com/andresdotsh/csrf-next-issue/tree/main) 👈
When I import the
csrf library in the middleware.js file, I get an error that says assert is not a function.This is because the [csrf](https://www.npmjs.com/package/csrf) library depends on the [rndm](https://www.npmjs.com/package/rndm) library, which uses NodeJS's
assert in its code.This error only occurs if I import it in the middleware.js file, it doesn't happen in page.js for example.
Does anyone have any idea why this might be happening? I would like to use this
csrf library, but this error is preventing me from doing so. Thanks in advance for any help 🙏.More info:
-
nodejs v20.15.1-
next@14.2.5-
csrf@3.1.0-
yarn@1.22.22- I'm using nextjs
app routerPictures:


Answered by andresdotsh
Someone already gave me the [answer here](https://github.com/vercel/next.js/discussions/68168). ✌️❤️
1 Reply
Someone already gave me the [answer here](https://github.com/vercel/next.js/discussions/68168). ✌️❤️
Answer