Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatsby cannot find fs despite npm install

I'm on:

  • Ubuntu 16.04
  • Gatsby CLI version: 2.7.14
  • Gatsby version: 2.13.21
  • Node v10.16.0
  • NPM 6.10.1

I'm following the Gatsby tutorial "7. Programmatically create pages from data". I can't solve this error:

This dependency was not found:
⠀
* fs in ./node_modules/electron/index.js,
⠀
To install it, you can run: npm install --save fs

I've run npm install --save fs already, and then tried it again. But the same error occurs. Permissions for the directory are set as a+rwx and everything has been installing fine up till this point.

This error happens when I add this code to gatsby-node.js as the tutorial says to:

const { createFilePath } = require(`gatsby-source-filesystem`)

exports.onCreateNode = ({ node, getNode }) => {
  if (node.internal.type === `MarkdownRemark`) {
    console.log(createFilePath({ node, getNode, basePath: `pages` }))
  }
}

The full flow of events when I run gatsby develop -p 8001:

success open and validate gatsby-configs - 0.070 s
success load plugins - 0.636 s
success onPreInit - 0.005 s
success initialize cache - 0.012 s
success copy gatsby files - 0.092 s
success onPreBootstrap - 0.026 s
success source and transform nodes - 0.145 s
success building schema - 0.560 s
success createPages - 0.003 s
success createPagesStatefully - 0.078 s
success onPreExtractQueries - 0.004 s
success update schema - 0.087 s
success extract queries from components - 0.340 s
success write out requires - 0.009 s
success write out redirect data - 0.006 s
success onPostBootstrap - 0.003 s
⠀
info bootstrap finished - 9.246 s
⠀
success run static queries - 0.051 s — 1/1 23.83 queries/second
success run page queries - 0.040 s — 3/3 135.00 queries/second
 ERROR  Failed to compile with 7 errors                     10:01:07 PM
⠀
This dependency was not found:
⠀
* fs in ./node_modules/electron/index.js,
⠀
To install it, you can run: npm install --save fs
✖ 「wdm」:
ERROR in ./node_modules/electron/index.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/electron'
 @ ./node_modules/electron/index.js 1:9-22
 @ ./node_modules/gatsby-source-filesystem/node_modules/got/index.js
 @ ./node_modules/gatsby-source-filesystem/create-remote-file-node.js
 @ ./node_modules/gatsby-source-filesystem/index.js
 @ ./src/pages/gatsby-node.js
 @ ./.cache/sync-requires.js
 @ ./.cache/app.js
 @ multi ./node_modules/event-source-polyfill/src/eventsource.js
(webpack)-hot-middleware/client.js?path=/__webpack_hmr&reload=true&over
lay=false ./.cache/app

ERROR in ./node_modules/fs-extra/lib/empty/index.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/fs-extra/lib/empty'
 @ ./node_modules/fs-extra/lib/empty/index.js 4:11-24
 @ ./node_modules/fs-extra/lib/index.js
 @ ./node_modules/gatsby-source-filesystem/index.js
 @ ./src/pages/gatsby-node.js
 @ ./.cache/sync-requires.js
 @ ./.cache/app.js
 @ multi ./node_modules/event-source-polyfill/src/eventsource.js
(webpack)-hot-middleware/client.js?path=/__webpack_hmr&reload=true&over
lay=false ./.cache/app

ERROR in ./node_modules/graceful-fs/graceful-fs.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/graceful-fs'
 @ ./node_modules/graceful-fs/graceful-fs.js 1:9-22
 @ ./node_modules/fs-extra/lib/fs/index.js
 @ ./node_modules/fs-extra/lib/index.js
 @ ./node_modules/gatsby-source-filesystem/index.js
 @ ./src/pages/gatsby-node.js
 @ ./.cache/sync-requires.js
 @ ./.cache/app.js
 @ multi ./node_modules/event-source-polyfill/src/eventsource.js
(webpack)-hot-middleware/client.js?path=/__webpack_hmr&reload=true&over
lay=false ./.cache/app

ERROR in ./node_modules/jsonfile/index.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/jsonfile'
 @ ./node_modules/jsonfile/index.js 5:8-21
 @ ./node_modules/fs-extra/lib/json/jsonfile.js
 @ ./node_modules/fs-extra/lib/json/index.js
 @ ./node_modules/fs-extra/lib/index.js
 @ ./node_modules/gatsby-source-filesystem/index.js
 @ ./src/pages/gatsby-node.js
 @ ./.cache/sync-requires.js
 @ ./.cache/app.js
 @ multi ./node_modules/event-source-polyfill/src/eventsource.js
(webpack)-hot-middleware/client.js?path=/__webpack_hmr&reload=true&over
lay=false ./.cache/app

ERROR in ./node_modules/md5-file/index.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/md5-file'
 @ ./node_modules/md5-file/index.js 4:9-22
 @ ./node_modules/gatsby-source-filesystem/create-file-node.js
 @
./node_modules/gatsby-source-filesystem/create-file-node-from-buffer.js
 @ ./node_modules/gatsby-source-filesystem/index.js
 @ ./src/pages/gatsby-node.js
 @ ./.cache/sync-requires.js
 @ ./.cache/app.js
 @ multi ./node_modules/event-source-polyfill/src/eventsource.js
(webpack)-hot-middleware/client.js?path=/__webpack_hmr&reload=true&over
lay=false ./.cache/app

ERROR in ./node_modules/read-chunk/index.js
Module not found: Error: Can't resolve 'fs' in
'/home/testjs/tutorial-part-four/node_modules/read-chunk'
 @ ./node_modules/read-chunk/index.js 2:11-24
ℹ 「wdm」: Failed to compile.

I googled this error and it pops up in several contexts, none of which match this case with Gatsby. I'm new to React and Gatsby (hence the tutorial), so I don't know how to deal with this yet. How should I fix it?

like image 920
nusantara Avatar asked Jul 15 '19 22:07

nusantara


People also ask

Do I need to npm install fs?

fs it's a native node. js module, you don't need install it.

Do I need to install fs module?

The fs module helps you to work around the files and directories to carry out different tasks, such as a store, access, read, write, rename, and others. There is no need to install fs in node js to use it.

Does Gatsby need node JS?

Gatsby is built using JavaScript, and requires the Node. js runtime.

What is Gatsby npm?

npm, or Node package manager, is the default package manager for the Node. js JavaScript runtime. It lets you install and update libraries and frameworks (dependencies) for Node-based projects, and interact with the npm Registry. You'll use npm to install and upgrade Gatsby and its plugins. npm is a command line tool.


Video Answer


1 Answers

Try to edit file gatsby-node.js (or create it in the source root directory) and add these lines:

exports.onCreateWebpackConfig = ({ actions, loaders, getConfig }) => {
    const config = getConfig()
    config.node = {
        fs: 'empty'
    }
}

This will modify your Webpack config and should solve the issue

like image 108
Karim Avatar answered Oct 12 '22 14:10

Karim