Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
If/Else Si el bloque no se activa con el módulo 'fs' del nodo

Cuando intento ejecutar mi aplicación CLI de ejemplo (parámetros creados con yargs para analizar cadenas de opciones), aparece este error en un seguimiento de pila gruesa: https://streamable.com/v4sih6 (grabación de video ya que es tan grande)

No sé qué estoy haciendo mal. Aquí está mi código ts:

 import type { Arguments, CommandBuilder } from 'yargs'; import chalk from 'chalk'; import * as logos from '../logos'; import fs from 'fs'; type Options = { componentName?: string; }; export const command: string[] = ["component", "c", "comp"]; export const desc: string = 'Creates a component. Use: "vcli component title" - this makes a new title component (optional).'; export const builder: CommandBuilder<Options, Options> = (yargs) => yargs .options({ componentName: { type: 'string' }, }); //TODO: Check if component exists export const handler = (argv: Arguments<Options>): void => { const { componentName } = argv; if (componentName !== undefined) { const timerMsg: string = `${logos.prefix} Component with name '${componentName}' has been successfully created in` console.time(chalk.green(timerMsg)); fs.mkdirSync(`./src/components/${componentName}`); fs.writeFileSync(`./src/components/${componentName}/index.tsx`, `import React from 'react';\n\nimport './${componentName}Styles.scss'`); console.timeEnd(chalk.green(timerMsg)); } else if (fs.existsSync(`./src/components/${componentName}`)) { console.log(chalk.red(`${logos.prefix} Component ${componentName} already exists!`)); } else { // Write an inquirer.js prompt here to ask for the name of the component :) } };

Gracias por la ayuda.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El error que está obteniendo es que el File already exists para src/components/testing

El código que está fallando es:

 fs.mkdirSync(`./src/components/${componentName}`);

Arreglar

Compruebe if not exists antes de escribir, es decir if (!fs.existsSync('./src/components/${componentName}')) debería estar antes de intentar crear el directorio.

about 4 years ago · Juan Pablo Isaza Report

0

Si su problema es que esta línea de código:

 fs.mkdirSync(`./src/components/${componentName}`);

le está dando un error porque el directorio ya existe, entonces simplemente puede detectar ese error específico, probarlo e ignorarlo si es ese error específico porque este no es realmente un problema por el que deba abortar.

 export const handler = (argv: Arguments<Options>): void => { const { componentName } = argv; if (componentName !== undefined) { const timerMsg: string = `${logos.prefix} Component with name '${componentName}' has been successfully created in` console.time(chalk.green(timerMsg)); try { fs.mkdirSync(`./src/components/${componentName}`); } catch(e) { // only throw error if the error is not because the directory // already exists if (e.code !== 'EEXIST') { throw e; } } fs.writeFileSync(`./src/components/${componentName}/index.tsx`, `import React from 'react';\n\nimport './${componentName}Styles.scss'`); console.timeEnd(chalk.green(timerMsg)); } else if (fs.existsSync(`./src/components/${componentName}`)) { console.log(chalk.red(`${logos.prefix} Component ${componentName} already exists!`)); } else { // Write an inquirer.js prompt here to ask for the name of the component :) } };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!