Fs.writeFile not working properly



I keep getting an error Cannot read property ‘writeFile’ of undefined .
When I call it from a function() works fine when not in function.

< !DOCTYPE html >


//filestream = require(‘fs’);
var fs = require(‘fs’);
var b = require(‘bonescript’)

		function recordToFilename() {
		var input = document.getElementById('filename');
		var fileName = input.value;
		var qw = fileName;
		var data1 = "jdsfhadj"
		if (fileName) {
		alert('Recording: ' + fileName);
		fs.writeFile('t-danny.txt', 'djdashfdsjhg');
		} else {
		alert('Please enter a filename!');

	<label for="filename">Filename</label>
	<input name="filename" id="filename" type="text">

		<a id="enter_button" onclick="recordToFilename();" href="javascript:void(0);"
				title="enter">enter name</a>
			<p id="demo"></p>



Please post your code so we can see what’s going wrong. It’s probably an issue with where you’re defining Fs.