10

I need to use require() in my file so I can use 'readline', but I also need to use import so I can use a module. But when I use require inside of my module it says require is not defined. Here is the code :

var r1 = require('readline')
import car from './carclass.mjs'
var input = ""
var questionToAsk = ""
const question1 = () => {
 return new Promise((resolve, reject) => {
 r1.question(questionToAsk, (answer) => {
 input = answer
 resolve()
 })
 r1.question()
 })
}
const main = async () => {
 console.log("What is the Brand?")
 await question1()
 var brandName = input
 console.log("What is the Model?")
 await question1()
 var modelName = input
 console.log("What is the Speed?")
 await question1()
 var carSpeed = input
 var newCar = new car(brandName,modelName,carSpeed)
 r1.close()
}
main()

How can I use require() and import in a module, or is there some other way I can approach this?

asked May 20, 2020 at 22:24
3
  • Why don't you import rl from 'readline'? Commented May 20, 2020 at 23:10
  • Because it says r1.question() isn't a function Commented May 20, 2020 at 23:14
  • I think that's because you were skipping the .createInterface() step. See the working code below in my answer. Commented May 20, 2020 at 23:24

3 Answers 3

4

to use import and require() you can try this

import { createRequire } from "module";
const require = createRequire(import.meta.url);
answered Apr 15, 2022 at 10:41
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
3
import { createRequire } from "module";
const require = createRequire(import.meta.url);

Use this code on the top of the file in which you want to use 'IMPORT' and 'Require' both

Tyler2P
2,37030 gold badges26 silver badges34 bronze badges
answered Oct 10, 2022 at 7:41

Comments

2

I'd suggest you just use import instead of require() if you're already in an ESM module file since require() only exists in CJS modules, not ESM modules. Here's a code example that works:

import readline from 'readline';
const rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
});
rl.question("What's up?\n", data => {
 console.log(`Got your result: ${data}`);
 rl.close();
});

In node v12.13.1, I run this with this command line:

node --experimental-modules temp.mjs

FYI, from your comments, it appears you were skipping the .createInterface() step.

answered May 20, 2020 at 23:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.