A
A
Asya2021-08-18 02:21:57
Node.js
Asya, 2021-08-18 02:21:57

How to write a string to a javascript file?

I'm trying to do like this:

var fs = require('fs');
fs.writeFile("./test.txt", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
});


but the following error occurs:
Uncaught TypeError: fs.writeFile is not a function

how to fix it?

package.json
{
  "name": "backend-with-webpack",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error\" ",
    "build": "webpack src/js/app.js dist/bundle.js",
    "start": "serve dist"
  },
  "devDependencies": {
    "babel-plugin-transform-regenerator": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "webpack": "^2.7.0",
    "fs": "^0.0.1-security"
  },
  "dependencies": {
    "@babel/polyfill": "^7.12.1",
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "browserify-fs": "^1.0.0",
    "fs": "^0.0.1-security",
    "serve": "^12.0.0"
  },
  "browser": {
    "fs": false
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question