首页 > 解决方案 > Webpacker 在 Docker/Rails 设置中找不到一些 node_modules

问题描述

我有一个新的 Rails 5.2.4.1 实例,使用--webpack=stimulus. Docker化了整个事情。使用stimulusjs 没有问题 -import { Application } from "stimulus"像它应该的那样工作。

然后我添加了 turbolinks,yarn add turbolinks然后添加了 github README 中描述的类似内容(如果我使用orimport Turbolinks from "turbolinks"无关紧要)。然后导致“编译失败”。错误。不知何故,它找不到模块。我有同样的问题,例如另一个名为“isotope-layout”的包。importrequire

这是错误日志:要点

当我ls node_modules在 webpacker docker 容器上时,turbolinks 和 isotope-layout 文件夹就在那里。

❯ docker-compose run webpacker bash

bash-5.0$ pwd
/home/deploy/app

bash-5.0$ ls node_modules/turbolinks/
CHANGELOG.md  README.md     package.json
LICENSE       dist          src

编辑:docker-compose down --remove-orphans在 node_modules 中触发turbolinks 和其他模块后,它会编译...缓存问题?

这是我当前的 Webpacker 和 Docker 配置文件:

Dockerfile:

FROM ruby:2.6.5-alpine
RUN apk update && \
    apk upgrade && \
    apk add --update \
    build-base \
    nodejs \
    nodejs-npm \
    mysql-dev \
    curl \
    imagemagick \
    tzdata \
    yarn \
    bash && \
    rm -rf /var/cache/apk/*

RUN addgroup -S admin -g 1000 && adduser -S -g '' -u 1000 -G admin deploy

ENV RAILS_ROOT /home/deploy/app
ENV RAILS_LOG_TO_STDOUT 1
ENV NPM_CONFIG_PREFIX=/home/deploy/.npm-global
ENV PATH=$PATH:/home/deploy/.npm-global/bin

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

USER deploy
RUN mkdir -p $RAILS_ROOT
WORKDIR $RAILS_ROOT
COPY --chown=deploy:admin Gemfile Gemfile.lock ./
RUN npm install -g yarn
RUN bundle install --jobs 20 --retry 5 --binstubs
COPY --chown=deploy:admin . ./
RUN yarn install --pure-lockfile

入口点.sh:

#!/bin/bash
set -e
rm -f /home/deploy/app/tmp/pids/server.pid
exec "$@"

码头工人-compose.yml:

version: '3'

services:
  webpacker:
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/home/deploy/app
      - /home/deploy/app/bin
      - /home/deploy/app/node_modules
    command: bash -c 'rm -rf /home/deploy/app/public/packs; /home/deploy/app/bin/webpack-dev-server'
    ports:
      - '3035:3035'

  app:
    build:
      dockerfile: Dockerfile
      context: .
    command: >
      bash -c "
        rm -f tmp/pids/server.pid &&

        # Run test server detached
        RAILS_ENV=test puma -b tcp://0.0.0.0:3001 -d &&

        # Run web server
        puma -C config/puma.rb
        "
    ports: ['3000:3000', '3001:3001']
    environment:
      - RAILS_ENV=development
      - NODE_ENV=development
    depends_on:
      - webpacker
    volumes:
      - .:/home/deploy/app
      - /home/deploy/app/bin
      - /home/deploy/app/node_modules

webpacker.yml:

default: &default
  source_path: app/webpacker
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: false

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  # Extract and emit a css file
  extract_css: true

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .mjs
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: false

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: webpacker
    port: 3035
    public: 0.0.0.0:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      aggregate_timeout: 300,
      poll: 1000,
      ignored: '**/node_modules/**'


test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

webpack/environment.js:

const { environment } = require('@rails/webpacker')

module.exports = environment

应用程序.js:

import Turbolinks from "turbolinks"

标签: ruby-on-railsdockerwebpackdocker-composewebpacker

解决方案


运行yarn add turbolinks(或任何yard add ..)后,您应该停止 webpacker 服务,然后重新启动它。

因为那,在你运行之后docker-compose down --remove-orphans一切正常。您删除所有服务并重新创建它。只需停止和启动会更快。


推荐阅读