尽管 npm install,Gatsby 还是找不到 fs

Posted

技术标签:

【中文标题】尽管 npm install,Gatsby 还是找不到 fs【英文标题】:Gatsby cannot find fs despite npm install 【发布时间】:2019-11-24 15:13:07 【问题描述】:

我在:

Ubuntu 16.04 Gatsby CLI 版本:2.7.14 盖茨比版本: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 的新手(因此是教程),所以我还不知道如何处理这个问题。我该如何解决?

【问题讨论】:

【参考方案1】:

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

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

这将修改您的 Webpack 配置,应该可以解决问题

【讨论】:

最终我认为这行不通,但这确实让我想到了正确的想法:***.com/a/58151834/1443496 这给了我以下错误:ValidationError: Invalid configuration object。 Webpack 已使用与 API 架构不匹配的配置对象进行初始化。 - configuration.node 具有未知属性“fs”。这些属性是有效的:object __dirname?, __filename?, global? -> 节点兼容性功能的选项对象。

以上是关于尽管 npm install,Gatsby 还是找不到 fs的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 npm run develop 命令运行 Gatsby 应用程序

npm install 成功,但 npm run start 无法找到 rimraf 和 react-scripts,尽管它们已安装

npm install vercel 因错误的节点版本而失败,尽管它是正确的

npm install 显示错误,找不到 git

尽管运行“npm i @types/node --global”,为啥我得到“找不到名称'NgModule'”?

npm install 说找不到文件