Nodemon error while running a ReadMe generator - javascript

Working on an assignment to write a ReadMe Generator,
but when I put in npm start I get the following error:
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\Franco\Desktop\Readme-Generator\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
This isn't code but Stack overflow is dumb sometimes
at file:///C:/Users/Franco/Desktop/Readme-Generator/index.js:1:18
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
[nodemon] app crashed - waiting for file changes before starting...
Here is my actual code for context
const inquirer = require('inquirer')
const questions = [
{
type: 'input',
name: 'title',
message: 'What is your project called?',
},
{
type: 'input',
name: 'description',
message: 'Please descbribe your project',
},
{
type: 'input',
name: 'installation',
message: 'Describe how you install the project here',
},
{
type: 'input',
name: 'usage',
message: 'how is your project used?',
},
{
type: 'input',
name: 'contribution',
message: 'who contributed?',
},
{
type: 'input',
name: 'test',
message: 'any tests?',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
},
{
type: 'input',
name: 'github',
message: 'enter your github username',
},
{
type: 'list',
name: 'License',
message: 'Choose your License',
choices: ['MIT', 'ISC', 'GNUPlv3'],
filter(val) {
return val.toLowerCase();
}
}
]
function runQuery() {
return inquirer.prompt(questions)
.then((answers) => {
console.log(answers)
return answers
})
.catch((error) => {
console.log(error)
})
}
runQuery
Does anyone have any idea what it could be?

Related

discord.js - TypeError [COMMAND_INTERACTION_OPTION_TYPE]: Option "user" is of type: STRING; expected USER

When I run the script for my discord bot and use /warn remove it keeps throwing an error that user is of type string expected USER. It says the error is happening between lines 69:42 and I can't find the issue.
module.exports = {
category: 'Moderation',
description: 'Warn a user',
permissions: ['ADMINISTRATOR'],
slash: true,
guildOnly: true,
options: [
{
type: 'SUB_COMMAND',
name: 'remove',
description: 'Removes a warning from the user',
options: [
{
name: 'user',
type: 'USER',
description: 'The user to remove the warning from',
required: true,
},
{
name: 'id',
type: 'STRING',
description: 'The ID of the warning to remove',
required: true,
},
],
}
],
callback: async ({ guild, member: staff, interaction }) => {
const user = interaction.options.getUser('user')
if (subCommand === 'remove') {
const warning = await warnSchema.findByIdAndDelete(id)
return {
custom: true,
content: `Removed warning ${warning.id} from <#${user?.id}>`,
allowedMentions: {
users: []
}
}
}
}
}
I didn't post all the code because it seemed like it would be too much searching but this is the code that is causing the error.

DiscordJS v13 Invalid Form Body

I am trying to make a toggle able slash command, if they pick the disable option it turns it off but when if you pick the enable option it asks to pick a channel but it gives this error
Error:
DiscordAPIError[50035]: Invalid Form Body
23.name[BASE_TYPE_REQUIRED]: This field is required
rawError: {
code: 50035,
errors: { '23': [Object] },
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'put',
url: 'https://discord.com/api/v9/applications/971024098098569327/commands'
Code:
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [
{
name: 'toggle',
description: 'Toggle On/Off The Welcomer',
type: 3,
required: true,
choices: [
{
name: 'disable',
value: 'off',
},
{
name: 'enable',
value: 'on',
choices: [
{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
},
]
},
],
},
],
Those would be an example of a subcommand and need to be indicated as such and will need descriptions in a couple places.
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [{
name: 'disable',
description: `'Disable welcomer`, // Added needed description
type: 1, //converted to subcommmand
}, {
name: 'enable',
description: `'Enable welcomer`, // Added needed description
type: 1, //converted to subcommmand
options: [{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
channel_types: [0] // allows only text channels to be selected
}]
}],
// run command pick only one of the below two
// if command.execute()
async execute(client, interaction, args)
// if command.run()
run: async (client, interaction, args) =>
// command code below here assumes you have the code in your `interactionCreate` listener to set up args
{
if (args.disable) {
// Code to turn off
} else if (args.enable) {
const channel = args.channel
// Code to turn on
};
}
}

useEffect continuously fires GET requests

I'm learning React and trying to make a small project by myself for the first time, but I'm having trouble with useEffect.
I'm trying to autofill a form with information from my backend. I can get it to autofill, but it continuously sends GET requests. This is what I have:
useEffect(() => {
axios
.get('/admin/edit-product' + location.search)
.then((res) => {
const updatedControls = {
...controlsState,
title: {
...controlsState.title,
value: res.data.title,
},
image: {
...controlsState.image,
value: res.data.image,
},
price: {
...controlsState.price,
value: res.data.price,
},
description: {
...controlsState.description,
value: res.data.description,
},
};
setControlsState(updatedControls);
})
.catch((err) => console.error(err));
}, [controlsState, location.search]);
I thought that the dependency array was supposed to stop it from running continuously, but I guess I'm missing something else.
Not sure if it's needed, but this is what my original state looks like:
const [controlsState, setControlsState] = useState({
title: {
elementType: 'input',
elementConfig: {
type: 'text',
},
label: 'Product Title: ',
value: '',
},
image: {
elementType: 'input',
elementConfig: {
type: 'url',
},
label: 'Image URL: ',
value: '',
},
price: {
elementType: 'input',
elementConfig: {
type: 'number',
},
label: 'Price: ',
value: '',
},
description: {
elementType: 'textarea',
elementConfig: {
name: 'description',
htmlFor: 'description',
},
label: 'Description: ',
value: '',
},
});
and location is from react-router-dom useLocation
You have given controlsState as a dependency for useEffect. But inside your useEffect you are using setControlsState which changes the value of controlsState. And since you have given controlsState as a dependency, useEffect will occur everytime any of its dependency changes. Hence it is repeatedly happening
If you want useEffect to run only once, give [] as second parameter:
useEffect(() => {
...your code...
}, [])

Unable to execute a promise in node.js

I am completely new to node.js, All I want to do is to use the inquirer NPM to ask a few questions, get the answers to those questions plug into a template literals and create a readme.md file from it. for some reason, my code doesn't work. Could someone point out what have I done wrong here? I feel like I'm missing some parameter when creating the promise maybe??
const inquirer = require("inquirer")
const fs = require("fs")
const util = require("util")
const readme = () => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\```
${data.usage}
\```
## License
${license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `
}
// array of questions for user
const questions = [
{
type: "input",
name: "title",
message: "What is the title of your project",
default: "Not Available"
},
{
type: "input",
name: "username",
message: "What is your Github username?",
default: "Not Available"
},
{
type: "input",
name: "email",
message: "What is your email address?",
default: "Not Available"
},
{
type: "input",
name: "contact",
message: "How do you want people to reach you?",
default: "Not Available"
},
{
type: "input",
name: "description",
message: "Please type a short description of your project ( When you are done, hit ESC & type \":wq\" to exit editor )",
default: "Not Available"
},
{
type: "input",
name: "installation",
message: "How do you install this application? ( When you are done, hit ESC type \":wq\" to exit editor) ",
default: "Not Available"
},
{
type: "input",
name: "usage",
message: "Give example of some ways you can use this application ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
},
{
type: "list",
name: "license",
message: "What kind of license would you like to use?",
choices: ["MIT","Microsoft Public License","Mozilla Public License 2.0","Academic Free License v3.0","Open Software License 3.0","Creative Commons Attribution 4.0"],
default: "Not Available"
},
{
type: "confirm",
name: "contribution",
message: "Are you open to contribution?",
default: "Not Available"
},
{
type: "input",
name: "contributionRequirement",
message: "If Yes, What are your requirement for giving contribution?",
default: "Not Available"
},
{
type: "input",
name: "test",
message: "Please give instructions for testing of this project ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
}
]
// function to write README file
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, "utf8", function(err) {
if (err) {
console.log(err);}
})}
// function to initialize program
const init = new Promise(function(resolve,reject) {
resolve(inquirer.prompt(questions))
})
// function call to initialize program
init.then(writeToFile("test.md",readme)
readme should run with result when inquirer get all input.
const inquirer = require ('inquirer');
const fs = require ('fs');
const util = require ('util');
const readme = data => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\`\`\`
${data.usage}
\`\`\`
## License
${data.license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `;
};
// array of questions for user
const questions = [
{
type: 'input',
name: 'title',
message: 'What is the title of your project',
default: 'Not Available',
},
{
type: 'input',
name: 'username',
message: 'What is your Github username?',
default: 'Not Available',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
default: 'Not Available',
},
{
type: 'input',
name: 'contact',
message: 'How do you want people to reach you?',
default: 'Not Available',
},
{
type: 'input',
name: 'description',
message: 'Please type a short description of your project ( When you are done, hit ESC & type ":wq" to exit editor )',
default: 'Not Available',
},
{
type: 'input',
name: 'installation',
message: 'How do you install this application? ( When you are done, hit ESC type ":wq" to exit editor) ',
default: 'Not Available',
},
{
type: 'input',
name: 'usage',
message: 'Give example of some ways you can use this application ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
{
type: 'list',
name: 'license',
message: 'What kind of license would you like to use?',
choices: [
'MIT',
'Microsoft Public License',
'Mozilla Public License 2.0',
'Academic Free License v3.0',
'Open Software License 3.0',
'Creative Commons Attribution 4.0',
],
default: 'Not Available',
},
{
type: 'confirm',
name: 'contribution',
message: 'Are you open to contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'contributionRequirement',
message: 'If Yes, What are your requirement for giving contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'test',
message: 'Please give instructions for testing of this project ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
];
// function to write README file
function writeToFile (fileName, data) {
fs.writeFile (fileName, data, 'utf8', function (err) {
if (err) {
console.log (err);
}
});
}
// function call to initialize program
inquirer.prompt(questions).then(answer => {
writeToFile('test.md', readme(answer))
});

inquirer package, present questions based on previous answers

I'm using NPM 'inquirer' package in order to present the user various questions. One of them is a 'choices' selection.
Is there a way to present follow up questions based on the 'choices' selection?
Here's my code:
const { prompt } = require('inquirer');
require('colors');
const questions = [
{
type: 'input',
name: 'appName',
message: 'Enter application name:'
},
{
type: 'input',
name: 'appDescription',
message: 'Enter app description:'
},
{
type: 'input',
name: 'appAuthor',
message: 'Enter app author:'
},
{
type: 'checkbox',
name: 'plugins',
message: 'Select plugins to install:',
choices: [
{
name: 'Cassandra'
}
]
}
];
module.exports.performQuestions = async () => {
console.log('\nWelcome to Chef!\n'.underline.italic.cyan);
const answers = await prompt(questions);
if (!answers.appName) {
console.error('\nPlease provide app name!\n'.red);
process.exit(1);
}
answers.appType = 'service';
return answers;
};
Here I want to present a few more questions if the user selects 'Cassandra' is that possible?
Thanks.
You can use "when" and like in the example bellow, the second question will popup only if "Cassandra" is selected:
const QUESTIONS = [
{
name: 'your-name',
type: 'list',
message: 'Your name:',
choices: ['Batman', 'Superman', 'Ultron', 'Cassandra'],
},
{
name: 'hello-cassandra',
type: 'confirm',
message: 'Oh, hello Cassandra!',
when: (answers) => answers['your-name'] === 'Cassandra',
},
];
inquirer.prompt(QUESTIONS)
.then(answers =>
{
console.log(answers);
});

Categories