尝试请求任何.md文件时出现raw-loader错误。
添加原始加载器以导入markdown文件:
test: /\.md$/i,
use: [{
loader: 'raw-loader',
options: {
esModule: false
}
}],在.js文件中,需要markdown文件。
return require(postPath)
// postPath is '../posts/awards.md'Error: Cannot find module '../posts/awards.md'
at webpackEmptyContext (eval at <path to file>)
....markdown文件的路径是相对路径:/posts/awards.md
如果我将awards.md更改为awards.json,它就可以工作。因此,也许是raw-loader在awards.md中查找export但找不到的问题,因此出错了?esModule: false的意义不就是告诉Webpack不要把它当做模块吗?
发布于 2020-06-01 10:03:07
看起来你和this person.遇到了同样的问题
引用一个答案:
Webpack在构建时执行静态分析。
它不会尝试推断导入(测试)变量可能是什么,因此导致了失败。
对于导入(path+“a.js”)也是如此。
如果需要真正的动态导入,则必须将其限制为一个已知路径:
import("./locales/" + locale + ".js")
我重现了你的问题,果然:
function test(mod) {
return require(mod)
}
console.log(test("./test.md"))不起作用。然而,是这样工作的:
function test(mod) {
return require("./" + mod)
}
console.log(test("test.md"))因此,将代码更改为以下代码就足够了:
return require("../" + postPath + ".md")并将postPath更改为:
// postPath is 'posts/awards'https://stackoverflow.com/questions/62123785
复制相似问题