js new file
时间: 2023-12-06 22:01:53 浏览: 32
To create a new file using JavaScript, you need to use the File System API. Here's an example code for creating a new file:
```javascript
const fs = require('fs');
fs.writeFile('newFile.txt', 'Hello World!', function (err) {
if (err) throw err;
console.log('File created successfully!');
});
```
In this code, we use the `writeFile` function from the `fs` module to create a new file called `newFile.txt` and write the text "Hello World!" into it. If the file already exists, it will be overwritten. The second parameter of the `writeFile` function is the content that will be written into the file.
When the file is successfully created, the callback function will be executed and the message "File created successfully!" will be printed in the console. If an error occurs, the `throw err` statement will throw an error and stop the execution of the program.