Uncaught SyntaxError: Unexpected token '<' bundle.js line 1 Docker 容器错误

时间:2021-02-11 22:13:44

标签: javascript node.js docker nginx webpack

我正在使用教程在线学习 Docker。我被要求构建的唯一内容是 Dockerfile,所以我确定问题出在那里,但我还无法找到一种有效的方法来调试问题。

问题是当我运行容器时,构建的 docker 图像 index.html 在 localhost 中呈现,但 bundle.js 不呈现并在控制台中给出标题中的错误。当我在本地机器上打包 webpack 并打开 index.html 时,网页会正确呈现。

Dockerfile:

FROM node:8.15-alpine as build-stage
COPY . /app
RUN npm install && npm start

FROM nginx:1.15
EXPOSE  80
COPY --from=build-stage /app /usr/share/nginx/html
COPY --from=build-stage /app/nginx.conf /etc/nginx/conf.d/default.conf

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Todo App</title>
    <link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
    <link rel="stylesheet" href="./styles/main.css" >
  </head>
  <body>
    <div id="content"></div>
    <script src="./bundle.js"></script>
  </body>
</html>

package.json:

{
  "name": "todos",
  "version": "1.0.0",
  "description": "This README would normally document whatever steps are necessary to get the application up and running.",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack --mode=development"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.2.2",
    "lodash": "^4.17.4",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  },
  "devDependencies": {}
}

webpack.config.js:

const path = require('path');

module.exports = {
  context: __dirname,
  entry: './frontend/todo_redux.jsx',
  output: {
    path: path.resolve(__dirname),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/env', '@babel/react']
          }
        },
      }
    ]
  },
  devtool: 'source-map',
  resolve: {
    extensions: [".js", ".jsx", "*"]
  }
};

nginx.conf

# This will tell our nginx server what path
# we want it to use for the html it renders
server {
  # list on port 80
  listen 80;
  location / {
    # to learn more about location blocks check out this resouce:
    # https://www.linode.com/docs/web-servers/nginx/how-to-configure-nginx/#location-blocks
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

更新:删除了我在 Dockerfile 的节点构建部分中的 CMD 行,将 RUN npm install 更改为 RUN npm install && npm start,现在正在创建 bundle.js 文件并将其添加到 nginx,但同样的错误仍然存​​在.

1 个答案:

答案 0 :(得分:0)

好吧,终于得到了教程制作者的帮助,发现我需要创建一个 WORKDIR /app,然后复制进去。对 Dockerfile 的以下更改解决了该问题:

FROM node:14.15-alpine as build-stage
WORKDIR /app
COPY . .
RUN npm install && npm start

我误读了 docs,我认为只有使用 COPY. /app 才能创建 /app 如果它没有找到它,但似乎在多阶段构建方面这还不够,需要创建 WORKDIR 才能被 --from=build-stage 正确引用。在那之后当然记得 COPY . . 而不是 COPY . /app 这也是我被困住了一段时间的地方。它似乎在图像中创建了一个额外的路径 /app/app,从而弄乱了最终渲染。

最后,从 node:8.15-alpinenode:14.15-alpine 的变化似乎不是一个因素,这是我在调试时进行的升级,只是为了看看它是否导致了问题。