最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

[Gatsby尽管安装了npm,但找不到fs

IT培训 admin 1浏览 0评论

[Gatsby尽管安装了npm,但找不到fs

我在:

  • Ubuntu 16.04
  • Gatsby CLI版本:2.7.14
  • Gatsby版本:2.13.21
  • 节点v10.16.0
  • NPM 6.10.1

我正在关注Gatsby教程"7. Programmatically create pages from data"。我无法解决此错误:

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

我已经运行npm install --save fs,然后再次尝试。但是会发生相同的错误。该目录的权限设置为a+rwx,到目前为止,所有内容都已安装完毕。

如本教程所述,当我将此代码添加到gatsby-node.js时,将发生此错误:

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

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

我运行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.

我在这个错误中进行了搜索,并在多个上下文中弹出,没有一个与Gatsby的情况相符。我是React和Gatsby的新手(因此是本教程),所以我还不知道如何处理。我应该如何解决?

回答如下:

尝试编辑文件gatsby-node.js(或在源根目录中创建它并添加以下行:

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

这将修改您的Webpack配置并应解决问题

[Gatsby尽管安装了npm,但找不到fs

我在:

  • Ubuntu 16.04
  • Gatsby CLI版本:2.7.14
  • Gatsby版本:2.13.21
  • 节点v10.16.0
  • NPM 6.10.1

我正在关注Gatsby教程"7. Programmatically create pages from data"。我无法解决此错误:

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

我已经运行npm install --save fs,然后再次尝试。但是会发生相同的错误。该目录的权限设置为a+rwx,到目前为止,所有内容都已安装完毕。

如本教程所述,当我将此代码添加到gatsby-node.js时,将发生此错误:

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

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

我运行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.

我在这个错误中进行了搜索,并在多个上下文中弹出,没有一个与Gatsby的情况相符。我是React和Gatsby的新手(因此是本教程),所以我还不知道如何处理。我应该如何解决?

回答如下:

尝试编辑文件gatsby-node.js(或在源根目录中创建它并添加以下行:

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

这将修改您的Webpack配置并应解决问题

发布评论

评论列表 (0)

  1. 暂无评论