如何打开和读取 Sapper/Svelte 中的文件

How to open and read a file in Sapper/Svelte

我正在尝试在我的 Sapper 应用程序中打开并读取 .md 文件目录。我试过 import 但它不允许字符串表达式 (`file${index}.md`)。所以我尝试通过 fs 节点模块打开和读取文件,但我在服务器中收到此消息:

preferring built-in module 'fs' over local alternative at 'fs', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning preferring built-in module 'fs' over local alternative at 'fs', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning 'fs' is imported by src/routes/blog/[slug].svelte, but could not be resolved – treating it as an external dependency 'default' is imported from external module 'fs' but never used

如果您正在访问文件系统,您应该在 服务器路由 中进行 — 即 .js 文件,如 routes/blog/[slug].json.js (对应于/blog/:slug.json) — 不在页面中,这将在服务器和客户端上 运行。

https://sapper.svelte.dev/docs#Server_routes