target: 'nwjs' with node.__filename: true, displays "Uncaught ReferenceError: __filename is not defined"
See original GitHub issueBug report
What is the current behavior?
Uncaught ReferenceError: __filename is not defined
/******/ /* webpack/runtime/readFile chunk loading */
/******/ (() => {
/******/ __webpack_require__.b = require("url").pathToFileURL(__filename);
This is the only instance of __filename in the whole generated bundle.
If the current behavior is a bug, please provide the steps to reproduce.
With the latest nwjs and webpack 5. My webpack.config.js is like this, but with loader rules:
module.exports = {
devtool: 'source-map',
entry: {
main: './src/main.js',
},
target: 'nwjs',
node: {
__dirname: true,
__filename: true
},
output: {
publicPath: 'src/',
filename: '[name]-bundle.js',
path: path.resolve(__dirname, ''),
},
};
I have my index.html and main.js in a src folder in the nwjs folder. The package.json that nwjs and my project looks like:
{
"name": "MyProject",
"main": "src/index.html",
"chromium-args": "",
"window": {
"kiosk": false,
"width": 600,
"height": 600,
"always_on_top": true
},
"webkit": {
"plugin": true
},
"node-remote": [],
"permissions": [
"cookies",
"serial",
"*"
],
"scripts": {
"build": "webpack --config webpack.config.js"
},
"dependencies": {
},
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"babel-loader": "^8.2.3",
"copy-webpack-plugin": "^9.0.1",
"css-loader": "^6.5.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.2.4",
"webpack": "^5.60.0",
"webpack-cli": "^4.9.1"
}
}
The index.html is very basic, but I’ll include it in case I’m doing something wrong:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>My Project</title>
<script type="module" src="main-bundle.js"></script>
<script>
document.addEventListener('DOMContentLoaded', async e => {
//...
});
</script>
</head>
<body></body>
</html>
What is the expected behavior?
I’m not sure on the specifics, but if I set __filename manually to ''
that tries to load resources from say:
file:///C:/Users/Username/Desktop/src/fonts/Roboto.woff
What I’d expect is:
file:///C:/Users/Username/Desktop/MyProject/src/fonts/Roboto.woff
Everything works if I manually change __filename to be './'
:
__webpack_require__.b = require("url").pathToFileURL('./');
So I’m guessing __filename should be set to ‘./’ as a global? That the bundle is generated with it undefined seems like maybe something recent was changed, so perhaps this is a regression. I didn’t test old versions of webpack or nw.js.
Other relevant information: webpack version: 5.60.0 and webpack-cli is 4.9.1 Node.js version: v14.18.0 Operating System: Ubuntu Additional tools:
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (7 by maintainers)
Top GitHub Comments
you probably want to use
mock
option, see https://webpack.js.org/configuration/node/For using ESM you should do more configurations https://webpack.js.org/configuration/experiments/#experimentsoutputmodule