Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  115] [ 3]  / answers: 1 / hits: 23343  / 8 Years ago, tue, march 8, 2016, 12:00:00

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.


More From » node.js

 Answers
11

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.


[#63009] Saturday, March 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
irvingcarloe

Total Points: 677
Total Questions: 109
Total Answers: 96

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
irvingcarloe questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Tue, Aug 4, 20, 00:00, 4 Years ago
Fri, Jul 3, 20, 00:00, 4 Years ago
;