Deke
Deke

Reputation: 4649

How do I access a javascript library file from my node_modules to my files in views

I am trying to access a javascript library file file from index.ejs.I have written a path this way in index.ejs:

<script src="../../node_modules/sweetalert/dist/sweetalert.min.js"></script>

But it is not working. I get an error saying cannot GET the resource. file structure below:

mainapp
|
|_ _ node_module -- sweetalert(folder) -- dist(folder) - -sweetalert.min.js
|
|
|_ _ views-- items(folder) -- index.ejs

the documentation says to use lib directory if on new version. I tried both as they suggested and I still get same error.

Upvotes: 0

Views: 487

Answers (2)

Nalla Srinivas
Nalla Srinivas

Reputation: 933

If you are using Express.js then configure like the below code in app.js file

 app.use("/lib", express.static(path.join(__dirname,'node_modules')));

Here, lib is the base path and then import this file under the node_modules as below

 <script src="/lib/sweetalert/dist/sweetalert.min.js" type="text/javascript"></script>

Be careful while adding the your file paht under the node_modules folder

Upvotes: 1

qbush
qbush

Reputation: 720

Put "sweetalert.min.js" into a "public" folder that is being hosted by your app, nginx, apache, etc... If you are hosting it from you app (which means you are using pure node.js, express.js, hapi.js, etc...) make sure you have the "public" folder hosted at the "/public" route or something similar.

Then when you need to access it, you can pull it from "your_domain/public/sweetalert.min.js". You can also make a "/scripts" subdirectory of the "/public" folder.

Upvotes: 0

Related Questions