__dirname在带有Webpack捆绑的节点js中不起作用 [英] __dirname is not working in node js with webpack bundling
问题描述
我当前的目录是
D:\bkp\Programming\TestWorks\nodejs\testApp
但是当我使用 __ dirname
并尝试使用Express Server显示文件时,它给了我这个错误
but when i am using __dirname
and trying to show a file with express server it gives me this error
Error: ENOENT: no such file or directory, stat 'D:\views\index.html'
我的代码是
res.sendFile(__dirname + 'views/index.html');
当我将其与webpack捆绑在一起并运行捆绑文件时,就会发生这种情况.否则,如果我只是运行普通的app.js文件,它将运行正常.帮助将不胜感激.
when i am bundling it with webpack and run the bundle file then this happens. Otherwise if i just run the normal app.js file it works fine. Help would be appreciated.
推荐答案
这是因为webpack可以以不同方式处理__dirname(和其他节点特定的事物).如果您希望它的行为像往常一样,请在您的webpack配置中使用它:
This is because webpack can handle __dirname (and other node specific things) in different ways. If you want it to behave like normal, use this in your webpack config:
{
node: {
__dirname: false
}
}
请参阅: https://webpack.js.org/configuration/node/
这篇关于__dirname在带有Webpack捆绑的节点js中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!