1

I'm new with Node js and basically, this is my first app in Node js and javascript. I have the following HTML file and try to load bootstrap CSS from node modules

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyApp</title>
    <link  href='../node_modules/bootstrap/dist/css/bootstrap.css' rel ="stylesheet" media="all">

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="/api/images">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
    </div>
  </nav>
</body>

</html>

I have tried :

<link  href='../node_modules/bootstrap/dist/css/bootstrap.css' media="all">

and

<link  href='../node_modules/bootstrap/dist/css/bootstrap.css'>

and

<link  href='../node_modules/bootstrap/dist/css/bootstrap.min.css' rel ="stylesheet" media="all">

no success though.

index.html is in the view folder and index.js is in the root folder.

bootstrap does not apply to the HTML file?

2 Answers 2

5

You could use a static folder, then link it in html file like this:

app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap

then add the link like:

<link rel="stylesheet" href="/css/bootstrap.min.css">
Sign up to request clarification or add additional context in comments.

2 Comments

worked! thanks man. I'm just wondering how it solves the problem? does it mean express js handles everything in the app?
When you are installing a package from npm, you are not installing it to use it directly from nod_modules. if so, it would be easy if you import it from cdn, also another concept that we should call files from public resources directory not private packages like node_module.
-2

Try loading bootstrap from the website

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyApp</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="/api/images">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
    </div>
  </nav>
</body>

</html>

It should work

1 Comment

should work locally, I've tried from maxcdn.bootstrapcdn.com, not working

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.