使用时:
import path from 'path';
path.resolve('/')
Run Code Online (Sandbox Code Playgroud)
我收到标题错误,但是当我使用
require('path').resolve('messages.json'))
Run Code Online (Sandbox Code Playgroud)
import { readFile, writeFile } from 'fs/promises';
import { v4 as uuidv4 } from 'uuid';
import path from 'path';
interface MessagesJson {
messages: Array<{ content: string; id: string }>;
}
export class MessagesRepository {
async findOne(id: string): Promise<string> {
return id;
}
async findAll(): Promise<any> {
return null;
}
async create(message: any): Promise<any> {
console.log(' dirname', require('path').resolve('messages.json'));
console.log(' path.resolve', path.resolve('/'));
// console.log(' path.resolve', path.resolve(__dirname, '/src'));
const messages: any = await readFile('src/messages.json', 'utf-8'); …Run Code Online (Sandbox Code Playgroud)