In my HTML file, I included a script like:
<script src="js/index.js"></script>
In my script, I try to add a configuration.js
file just by writing const Configuration = require("./configuration");
. configuration.js
is in the same folder with index.js
. But on console it says:
Uncaught Error: Cannot find module './configuration'
configuration.js and index.js files are both in /app/js/
folder.
What is a solution to that? I can include Node.js modules like Lodash for example.
If you want to understand what is happening when you require a module, you could read the docs.
On the first look, your code snippets should work. To require a module inside node.js, you always use the path from the file.
But, in your case, you are just importing plain JS. In this case, the script runs out of your HTML call.
That logic could cause a lot of other problems, so I would recommend you to create your own modules. node.js makes that very easy.
var configuration = {a: 1,b: 2};
module.exports = configuration;
More reading aout that:
Inside your HTML file, you could put your modules together via require statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With