【问题标题】:Cannot find module "fs". Using simple-json-loader找不到模块“fs”。使用 simple-json-loader
【发布时间】:2017-11-01 17:26:38
【问题描述】:

我正在尝试使用simple-json-loader。我从 npm 安装它并编写以下函数:

onclickExport() {
    var loader = require('simple-json-loader');
    var json = loader.JSONLoader.loadFromFile('wwwroot/dist/data/files.json');
}

一切看起来很简单,但是当我在 webback 中运行构建时,我看到以下错误:

./~/simple-json-loader/index.js 中的错误 找不到模块:错误:无法解析 D:\GitRepo\Magazine\Magazine.Web\node_modules\simple-json-loader 中的模块“fs” @ ./~/simple-json-loader/index.js

Npm 包通过有效路径位于 node_modules 中。我在那里看到。 此外,在调试此功能时,我看到了类似的错误。 有什么想法为什么它不起作用?

【问题讨论】:

  • 所以它是一个服务器端包,除非您使用类似BrowserFS 的东西,否则它不能在客户端(在浏览器中)工作。

标签: typescript webpack node-modules fs angular2-components


【解决方案1】:

你需要在 webpack 配置中指定target

target: 'node'

这只能在服务器端作为 node.js 程序工作。

【讨论】:

    【解决方案2】:

    听起来问题在于模块simple-json-loader 依赖于内置的Node 模块fsfs 是一个执行各种文件加载和文件系统任务的模块。

    由于您似乎是在 Angular2 中构建的,它是一个前端(客户端)Javascript 框架,这可能是一个环境问题。

    客户端浏览器环境无法解释和运行某些 Node 模块,例如 fs,这些模块是为在服务器端 Node 环境中运行而构建的。 (more here)

    解决方案是在 Node 环境中运行simple-json-loader 模块服务器端,或者找到一个提供相同功能但为浏览器编写的包。

    在这个问题中讨论过...... Module not found: Error: Cannot resolve module 'fs'

    而这个问题... Use fs module in React.js,node.js, webpack, babel,express

    【讨论】:

    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 2023-01-28
    • 2017-02-12
    • 2017-09-06
    • 2017-04-15
    • 1970-01-01
    • 2020-01-29
    • 2019-06-11
    相关资源
    最近更新 更多