'Writing an array to a file in javascript [duplicate]

I have an array of 32 strings called "arr" and I'm trying to save it to a file (like a txt file). I'm doing this in javascript because I'm getting this array from an html page.

I've tried this solution : node.js - how to write an array to file

So this code :

var fs = require('fs');

var file = fs.createWriteStream('array.txt');
file.on('error', function(err) { /* error handling */ });
arr.forEach(function(v) { file.write(v.join(', ') + '\n'); });
file.end();

results for me this error :

Error: Module name "fs" has not been loaded yet for context: _. Use require([])

I saw a solution there : https://requirejs.org/docs/errors.html#scripterror and tried this one :

require(['foo'], function (foo) {
    //foo is now loaded.
});

but it still results 3 errors :

  • "Failed to load resource: the server responded with a status of 404 (Not Found)"
  • "Refused to execute http://[...]/scripts/fs.js as script because "X-Content-Type-Options: nosniff" was given and its Content-Type is not a script MIME type. defaultOnError — require.js:5:1663"
  • "Error: Script error for "fs""

I'm a beginner in Javascript and I don't really understand how to use require.js. I tried to find a solution but I everything resulted to those errors.

Could you help me to write arrays in a text file ? Thank you for your help !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source