简体   繁体   English

将Bootstrap添加到index.js

[英]Adding Bootstrap to index.js

I'm stuck trying to add Bootstrap to my index.js file as per instructions on Bootstrap's site. 我一直在尝试按照Bootstrap网站上的说明将Bootstrap添加到我的index.js文件中。 Their suggestion errors out when I try it. 当我尝试时,他们的建议出错了。 I'm pretty sure there's something I'm missing, but I don't know what. 我很确定有什么我想念的,但是我不知道是什么。

index.js is: index.js是:

const express = require('express');
const hbs = require('hbs');
import 'bootstrap';

const app = express();

hbs.registerPartials(__dirname + '/views/partials');
app.set('view engine', 'hbs');

app.get('/', (req, res) => res.render('index'));

app.listen(3000, () => {
  console.log('App listening at port 3000!')
});

Error out is: 错误输出是:

import 'bootstrap';
^^^^^^

SyntaxError: Unexpected token import

My package.json is: 我的package.json是:

"dependencies": {
    "bootstrap": "^4.0.0",
    "express": "^4.16.2",
    "hbs": "^4.0.1",
    "jquery": "^3.3.1",
    "popper.js": "^1.12.9"
  },

Hmm! 嗯! When I added the following to the head of my index.hbs file it seems that bootstrap works for rendering the navbar properly now, but I'd like to achieve the same by using bootstrap installed through node npm. 当我将以下内容添加到index.hbs文件的开头时,引导程序现在可以正常显示导航栏了,但是我想通过使用通过节点npm安装的引导程序来实现相同的目的。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

You can't use imports in nodejs environment if you don't use babel. 如果不使用babel,则不能在nodejs环境中使用imports You should use require . 您应该使用require

Also, you are trying to import bootstrap in your server.js file. 另外,您正在尝试在您的server.js文件中导入bootstrap

I believe you want to import bootstrap in your app.js file. 我相信您想在您的app.js文件中导入bootstrap Your entry javascript file. 您的输入javascript文件。

In your case I would just use CDN : 在您的情况下,我只会使用CDN

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

If you want to use the import keyword, you either have to use babel to transpile the code, OR use the .mjs file format. 如果要使用import关键字,则必须使用babel来转换代码,或者使用.mjs文件格式。 But this is not the main issue in your code. 但这不是代码中的主要问题。

You are trying to import bootstrap to NodeJs, which doesn't make much sense. 您正在尝试将引导程序导入到NodeJ中,这没有什么意义。 NodeJs is server side javascript, bootstrap is for the frontend. NodeJs是服务器端javascript,引导程序用于前端。

You should be using some kind of view engine in your application for handling frontend components. 您应该在应用程序中使用某种视图引擎来处理前端组件。 Checkout embedded javascript or pug for example. 例如,检出embedded javascriptpug

However, nowadays people tend to use a frontend framework instead. 但是,如今人们倾向于使用前端框架来代替。 Checkout popular choices like React, Vue, or Angular. 检出常用的选择,例如React,Vue或Angular。 React has a tool called create-react-app which makes it really easy to get started with. React有一个叫做create-react-app的工具,它使上手变得非常容易。

Lastly, if you can give some insight on what you are trying to do with bootstrap, we may be able to give some more help. 最后,如果您可以对使用Bootstrap所做的事情有一些了解,我们也许可以提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM