3

Getting the following error while doing yarn generate in production in a Kubernetes container. It is working fine on local. And it was also working fine on production till last week.

Error: Cannot find module 'node:fs'
Require stack:
- /usr/app/node_modules/rc9/dist/index.cjs
- /usr/app/node_modules/@nuxt/config/dist/config.js
- /usr/app/node_modules/@nuxt/cli/dist/cli-index.js
- /usr/app/node_modules/@nuxt/cli/dist/cli.js
- /usr/app/node_modules/nuxt/bin/nuxt.js

I already have the following fix implemented in nuxt.config.js file.

config.node = {
    fs: 'empty',
}

VERSIONS

node:14.17.5-alpine

nuxt": ^2.15.8

Thank you for you guidance.

I tried using an upgraded image of Node.js (16.5.0-alpine), but that started giving another error while building.

sass-loader not found 
Syed Ahmed
  • 112
  • 1
  • 7
  • It seems that you are facing two different issues. The first issue with the error message "Cannot find module 'node:fs'" is related to the missing 'fs' module in your Node.js environment. The solution you mentioned in your nuxt.config.js file is the correct approach to handle this error by setting the fs property to 'empty'. Regarding the second issue with the error message "sass-loader not found," it indicates that the sass-loader module is not installed in your environment. This issue can be resolved by installing the missing package by running the following command: yarn add sass-loader – Bouramas Feb 10 '23 at 07:05
  • If you are still facing issues, I would recommend checking if all the required dependencies are installed and present in your node_modules directory. You can do this by checking the dependencies section of your package.json file and running yarn install to install any missing dependencies. – Bouramas Feb 10 '23 at 07:05
  • Hi @Bouramas, thanks for responding. Second issue is only coming if I upgrade node to version 16.x.x But I want to keep Node to version 14.x.x. And even after applying the fs: empty fix. it is still causing the "Cannot find module node:fs" error – Syed Ahmed Feb 10 '23 at 09:43
  • `fs: 'empty'` doesn't work for me, I have to upgrade nodejs to 18.16.0. – Capt. Michael Jun 13 '23 at 16:01

3 Answers3

2

I had the exact same problem with nuxtjs and solved it like this way:

  1. I tried to update my nodeJs to the last stable version:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Based on this: How can I update my nodeJS to the latest version?

  1. Then, I've done this:
export NODE_OPTIONS=--openssl-legacy-provider

Based on this error : https://github.com/webpack/webpack/issues/14532#issuecomment-947012063

However, I can't really understand why it worked, if someone could edit and give more details.

Ryu Roman
  • 43
  • 1
  • 6
0

I had the similar error on a fresh nuxt.js project, solved by upgrading the node version to 16.13.1

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 
source ~/.profile 
nvm install 16.13.1 
nvm use 16.13.1
Sazzad
  • 176
  • 1
  • 9
0

I had similar error with Nuxt 2 production build when updating NodeJS version and I solve it by upgrade nuxt-start package:

yarn add nuxt-start
Alex K
  • 2,613
  • 1
  • 20
  • 31