Mar*_*cco 1 node.js npm enoent
我一直遇到这个错误,我不知道该怎么办。这是我的代码:
const fs = require('fs');
const archivo = fs.readFileSync('C:\Users\marco\OneDrive\Desktop\Code\WebDeveloping\txt\test.txt');
console.log(archivo);
Run Code Online (Sandbox Code Playgroud)
这是错误:
Error: ENOENT: no such file or directory, open 'C:UsersmarcoOneDriveDesktopCodeWebDeveloping xt est.txt'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.<anonymous> (C:\Users\marco\OneDrive\Desktop\Code\WebDeveloping\js\index.js:57:20)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: 'C:UsersmarcoOneDriveDesktopCodeWebDeveloping\txt\test.txt'
}
Run Code Online (Sandbox Code Playgroud)
所有文件都存在,这就是它们的正确路径
您必须使用双反斜杠才能在字符串中得到一个反斜杠。这是因为反斜杠是 Javascript 字符串中的转义字符,因此要在字符串中实际获得反斜杠,您需要连续使用两个反斜杠:
改变这个:
const archivo = fs.readFileSync('C:\Users\marco\OneDrive\Desktop\Code\WebDeveloping\txt\test.txt');
Run Code Online (Sandbox Code Playgroud)
对此:
const archivo = fs.readFileSync('C:\\Users\\marco\\OneDrive\\Desktop\\Code\\WebDeveloping\\txt\\test.txt');
Run Code Online (Sandbox Code Playgroud)
您可以在错误行中看到问题:
Error: ENOENT: no such file or directory, open 'C:UsersmarcoOneDriveDesktopCodeWebDeveloping xt est.txt'
Run Code Online (Sandbox Code Playgroud)
您将看到该路径中没有反斜杠,并且额外的空格被\t解释为制表符。
| 归档时间: |
|
| 查看次数: |
7984 次 |
| 最近记录: |