Next.js Discord

Discord Forum

assert is not a function error, when import csrf in middleware.js

Answered
andresdotsh posted this in #help-forum
Open in Discord
Avatar
andresdotshOP
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 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 router

Pictures:

![Error example](https://i.ibb.co/dgZF8kf/err1.png)

![Error example 2](https://i.ibb.co/JqXPsNN/err2.png)
Answered by andresdotsh
Someone already gave me the [answer here](https://github.com/vercel/next.js/discussions/68168). ✌️❀️
View full answer

1 Reply

Avatar
andresdotshOP
Someone already gave me the [answer here](https://github.com/vercel/next.js/discussions/68168). ✌️❀️
Answer