0

My question is straight forward

var path = './App.vue'

//var App = require(path);  //THIS WILL NOT WORK
var App = require('./App.vue'); //THIS WORKS

DEMO HERE : require() does not accept javascript variable having string

If I want to make it work as to accept javascipt varibales as strings, what I should do.

2
  • 1
    Generally, you shouldn’t do that. Commented Aug 13, 2019 at 22:33
  • Why would you want to do this in the first place? Commented Aug 13, 2019 at 22:38

1 Answer 1

0

That's a good question. I know webpack doesn't like it when you do it because I read this:

Using require('...') with a variable vs. using a string in webpack

But I also think it's weird that this works:

var App = require(`${path}`);

I'm just curios, why do you need it to be a variable? The reason it might not work is a tool like webpack would need to know the path before it ran any code so that it can bundle all your modules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.