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

Ped*_*osa 1 io node.js rollupjs svelte sapper

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

\n\n

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

\n

Ric*_*ris 5

如果您正在访问文件系统,则应该在服务器路由\xe2\x80\x94 即 .js 文件中执行此操作,例如routes/blog/[slug].json.js(对应于/blog/:slug.json) \xe2\x80\x94 而不是在页面中,这将在服务器和客户端上运行。

\n\n

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

\n