为什么我不能在docker中构建这个nuxtjs应用程序,而本地构建工作?

时间:2018-06-04 14:08:36

标签: webpack sass nuxt.js

我正在Mac上开发这个nuxtjs项目(所有最新和最好的),本地构建和生成工作正常。我使用sass-loader作为dev依赖项,我的nuxt.config.js文件与默认文件没有太大区别:

module.exports = {
  head: {
    title: 'temp-www',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: 'Nuxt.js project' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },
  modules: [
    [ 'nuxt-fontawesome', {
      component: 'fa',
      imports: [
        { set: '@fortawesome/fontawesome-free-brands' },
      ]
    }],
  ],
  loading: { color: '#3B8070' },
  build: {
    extend (config, { isDev, isClient }) {
      if (isDev && isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }
}

然后我创建了一个Dockerfile这样的

FROM node:stretch

ENV NODE_ENV=production
ENV HOST=0.0.0.0

RUN mkdir -p /app
COPY . /app
WORKDIR /app

EXPOSE 3000

RUN npm install
RUN npm run build
CMD ["npm", "start"]

(和适当的.dockerignore)但是在构建图像时出现以下错误。

ERROR in ./layouts/default.vue
Module not found: Error: Can't resolve 'sass-loader' in '/app/layouts'
 @ ./layouts/default.vue 2:2-436
 @ ./.nuxt/App.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js

在那里,在其他.vue文件中,我在像<style lang="scss">这样的块中使用sass。我尝试将其更改为sass,但没有任何区别。

奇怪的是,this question说它应该通过将sass-loadernode-sass作为运行时deps而不是devtime deps来工作。我怀疑地尝试了这一点,并且情况变得更糟,直到我将lang块中的style属性更改为scss而不是sass。在这种情况下,仅这种情况下,我正确地构建了图像。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

you have to install sass-loader for webpack:

npm install sass-loader --save-dev

(see the official doc about "How to use pre-processors" (eg. sass): https://nuxtjs.org/faq/pre-processors/ )


But setting environment variable NODE_ENV to "production" causes npm install to ignore devDependencies.

So you can install as main dependency:

npm install sass-loader --save

The right way is to continue using as dev dependency and build your app on CI and then deploy it on your production env with NODE_ENV "production"