Node/Docker is node-sass 未找到已安装的绑定(通过 Webpack)

Node/Docker is node-sass not finding installed bindings (via Webpack)

我正在努力在基于 node:latest

的 docker 容器中设置 webpack 开发服务器

尽管尝试了 Node Sass could not find a binding for your current environment 中的所有各种咒语,但我仍然收到相同的错误:

web_1         | ERROR in ./~/css-loader!./~/sass-loader/lib/loader.js!./src/sass/style.sass
web_1         | Module build failed: Error: Missing binding /prject/node_modules/node-sass/vendor/linux-x64-59/binding.node
web_1         | Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 9.x
web

这是当前的

# Dockerfile
RUN yarn cache clean && yarn install --non-interactive --force
RUN rm -rf node_modules/node_sass
RUN npm rebuild node-sass

重建步骤表明已安装二进制文件 并签出:

Binary found at /advocate/node_modules/node-sass/vendor/linux-x64-59/binding.node
Testing binary
Binary is fine

同样让我感到困惑的是我确实得到了这个

web_1         | Found bindings for the following environments:
web_1         |   - OS X 64-bit with Node.js 7.x

这让我觉得它正在以某种我不太了解的能力使用主机平台。

node_modules 目录的状态正在从开发主机获取到容器中。这是在 npm/yarn install 期间做出基于平台的决策时的问题,通常是使用本机代码的模块。

Dockerfile 构建

node_modules 添加到您的 .dockerignore 文件。在容器中安装会花费更长的时间,但你永远不应该在你的开发环境和容器之间有任何交叉。

已安装的开发卷

将带有 node_modules 的开发代码安装到容器中也会导致同样的问题。 运行一个yarn install --force在新平台上用之前应该正常切换一下就可以了over/back.

没有简单的方法可以在装载卷时忽略目录。您可以单独安装项目中的每个 directory/file,然后忽略 node_modules,但这需要大量工作。

同步开发卷

这是一种避免安装卷的方法。 docker-sync has an rsync strategy 表示可以忽略文件。这也可能会加快某些具有繁重文件访问模式的应用程序的速度。从 osx > vm > docker 装载的卷上的文件访问速度很慢。

version: '2'

options:
  verbose: true

syncs:
  yourproject_data:
    sync_strategy: 'rsync'
    src: './'
    sync_host_port: 10872
    sync_args: '-v' 
    sync_excludes:
      - '.sass-cache/'
      - 'sass-cache/'
      - 'vendor/'
      - 'bower_components/'
      - 'node_modules/'
      - '.git/'

虽然默认情况下文件删除不会同步到容器,但您需要考虑到这一点。当我需要清理东西时,我偶尔会删除同步卷。

试试这个命令:

sudo npm -g 配置设置用户 root

我也遇到了同样的问题。通过将 npm 用户设置为 root 用户来修复。