我正在努力在基于 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 安装
(通常是使用 native 代码的模块)期间做出基于平台的决策时,这是一个问题。
Dockerfile 构建
将 node_modules
添加到您的 .dockerignore
文件中。在容器中安装将花费更长的时间,但您不应该在开发环境和容器之间出现任何交叉。
已安装的开发卷
将带有 node_modules
的开发代码安装到容器中也会导致同样的问题。在使用新平台之前运行 yarn install --force
通常足以将其切换/返回。
安装卷时没有一种简单的方法可以忽略目录。您可以单独挂载项目中的每个目录/文件,然后忽略node_modules
,但这需要大量工作。
同步开发量
这是避免安装卷的一种方法。 docker-sync有一个 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/'
默认情况下,文件删除不会同步到容器,您需要考虑这一点。当我需要清理内容时,我偶尔会删除同步卷。
关于node.js - Node/Docker 是 node-sass 找不到已安装的绑定(bind)(通过 Webpack),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138931/