我无法在 Node.js 中使用 fs.writeFileSync() 创建文件,不知道为什么?
const fs = require('fs');
const folderName = process.argv[2] || 'Project'
fs.mkdirSync(folderName)
fs.writeFileSync(`${folderName}/index.html`)
fs.writeFileSync(`${folderName}/style.css`)
fs.writeFileSync(`${folderName}/app.js`)
这是我执行时遇到的错误:node样板.js Portfolio
文件夹“Portfolio”已创建,但未创建文件
const fs = require('fs');
const folderName = process.argv[2] || 'Project'
fs.mkdirSync(folderName)
fs.writeFileSync(`${folderName}/index.html`)
fs.writeFileSync(`${folderName}/style.css`)
fs.writeFileSync(`${folderName}/app.js`)
This is the error I am getting when I execute:node boilerplate.js Portfolio
the folder "Portfolio" is made but not the files????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过
fs.writeFileSync
< /a>?它表示
fs.writeFileSync(file, data[, options])
– 括号表示可选参数,这意味着只有options
是可选的,因此您必须始终传入文件
和数据
。您没有传递
数据
,并且错误消息通过未定义
暗示了这一点。如果您想要空文件,请传入一个空字符串作为
data
。Have you looked at the documentation for
fs.writeFileSync
?It says
fs.writeFileSync(file, data[, options])
– the brackets denote optional parameters and that means onlyoptions
is optional, so you must always pass infile
anddata
.You aren't passing
data
, and the error message alludes to that withundefined
.If you want empty files, pass in an empty string as
data
.