question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

target: 'nwjs' with node.__filename: true, displays "Uncaught ReferenceError: __filename is not defined"

See original GitHub issue

Bug 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:closed
  • Created 2 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
vankopcommented, May 19, 2022

you probably want to use mock option, see https://webpack.js.org/configuration/node/

1reaction
alexander-akaitcommented, Oct 29, 2021

For using ESM you should do more configurations https://webpack.js.org/configuration/experiments/#experimentsoutputmodule

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: filename is not defined - Stack Overflow
i set the location of myfile.html inside public/js and i executed /js/myfile.html and it worked, i don't get the reason of the error...
Read more >
ReferenceError - filename is not defined in ES module scope
Join this channel to get access to perks:https://www.youtube.com/channel/UCoSpmr2KNOxjwE_B9ynUmig/joinMy GearCamera ...
Read more >
Re: TypeScript application and Webpack bundling
esModule: true ... Line 11: Please note that the target is set to "node-webkit". ... Uncaught ReferenceError: process is not defined.
Read more >
Use Native Node Modules - NW.js Documentation
Make sure you rebuilt all native modules. Or you will meet various issues, even crashes. Once manually rebuild don't work for you, try...
Read more >
Errors | Node.js v19.3.0 Documentation
For all EventEmitter objects, if an 'error' event handler is not provided, the error will be thrown, causing the Node.js process to report...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found