Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Javascript, using require() and import in the same file

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?

Answer*

Draft saved
Draft discarded
Cancel
1
  • 2
    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. Commented Apr 15, 2022 at 12:32

lang-js

AltStyle によって変換されたページ (->オリジナル) /