How can I write to the NUL device under Windows from node.js?

后端 未结 2 1145
你的背包
你的背包 2021-02-08 08:17

This is bugging me for several days now. I know about the standard stream redirection to the NUL device, but this isn\'t the case. node.js uses CreateFileW under its fs native/l

2条回答
  •  遥遥无期
    2021-02-08 09:03

    Valid path to NUL device is "\\\\.\\NUL", not NUL, so the usage is: fs.writeFileSync("\\\\.\\NUL", "foo"). This issue was raised against Node.js on GitHub: https://github.com/nodejs/node-v0.x-archive/issues/9271

    Since NUL is a device, not a file, it has to be accessed via device namespace - this requires putting \\.\ in the beginning (the other slashes are for escaping) - see https://msdn.microsoft.com/en-gb/library/windows/desktop/aa365247.aspx#Win32_Device_Namespaces.

    There is also a simple dev-null library on NPM that can be used with streams: https://www.npmjs.com/package/dev-null (not with .writeFile though).

提交回复
热议问题