关于typescript:找不到模块” fs”。使用simple-json-loader

Cannot find module “fs”. Using simple-json-loader

我尝试使用simple-json-loader。我从npm安装此文件并编写以下函数:

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

D?一切似乎都很简单,但是当我在webback中运行build时,我看到以下错误:

ERROR in ./~/simple-json-loader/index.js
Module not found: Error: Cannot resolve module 'fs' in D:\\GitRepo\\Magazine\\Magazine.Web\
ode_modules\\simple-json-loader
@ ./~/simple-json-loader/index.js

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


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

由于看起来您是在Angular2(这是一个前端(客户端)Javascript框架)中构建的,因此这可能是环境问题。

客户端浏览器环境无法解释和运行某些诸如fs之类的Node模块,这些模块是在服务器端Node环境中运行的。 (更多在这里)

解决方案是在节点环境中在服务器端运行simple-json-loader模块,或查找提供与浏览器相同功能的软件包。

在此问题中进行了讨论...
找不到模块:错误:无法解析模块'fs'

这个问题...
在React.js,node.js,webpack,babel,express

中使用fs模块


您需要在webpack配置中指定target

1
target: 'node'

这仅在服务器端作为node.js程序起作用。