1

我正在使用 nodejs 4.2.4 和使用 sass 的 expressjs 设置一个 docker 容器。

我的 Dockerfile:

FROM node:4.2.4-wheezy
COPY myapp /srv/www/
RUN rm -r /srv/www/node_modules || echo "Not removing /srv/www/node_modules,     directory did not exist"

#RUN npm update
#RUN npm install -g --silent node-sass
RUN cd /srv/www && npm install --silent
#RUN cd /srv/www && npm rebuild node-sass

EXPOSE 3000

express.js 项目是使用他们的生成器生成的

 express -css sass --hbs myapp

它使用这个 package.json 生成一个骨架项目:

{
 "name": "myapp",
 "version": "0.0.0",
 "private": true,
 "scripts": {
   "start": "node ./bin/www"
  },
   "dependencies": {
   "body-parser": "~1.13.2",
   "cookie-parser": "~1.3.5",
   "debug": "~2.2.0",
   "express": "~4.13.1",
   "hbs": "~3.1.0",
   "morgan": "~1.6.1",
   "node-sass-middleware": "0.8.0",
   "serve-favicon": "~2.3.0"
  }
}

在我的带有节点 4.2.4 的 mac 上运行它可以正常工作,但在 docker 容器中运行它会出现以下错误:

philipp@Philipps-MacBook-Pro:~/node/expressv1 $docker run -it -P -w="/srv/www/" test_server_7 npm start 
npm info it worked if it ends with ok
npm info using npm@2.14.12
npm info using node@v4.2.4
npm info prestart myapp@0.0.0
npm info start myapp@0.0.0

> myapp@0.0.0 start /srv/www
> node ./bin/www

/srv/www/node_modules/node-sass-middleware/node_modules/node-sass/lib/extensions.js:158
    throw new Error([
    ^

Error: The `libsass` binding was not found in /srv/www/node_modules/node-sass-middleware/node_modules/node-sass/vendor/linux-x64-46/binding.node
This usually happens because your node version has changed.
Run `npm rebuild node-sass` to build the binding for your current node version.
    at Object.sass.getBinaryPath (/srv/www/node_modules/node-sass-middleware/node_modules/node-sass/lib/extensions.js:158:11)
    at Object.<anonymous> (/srv/www/node_modules/node-sass-middleware/node_modules/node-sass/lib/index.js:16:36)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/srv/www/node_modules/node-sass-middleware/middleware.js:3:12)
    at Module._compile (module.js:435:26)

但是,如果我从 5.4.1-wheezy 扩展 docker 映像,它可以正常工作。libsass 和 node 似乎存在相当多的问题(这里这里这里)。我尝试了这些链接中的所有建议,其中一些仍然保留在上面的 Dockerfile 中,但它们都没有任何效果。在某种程度上,如果我来自干净的节点安装,我将不得不重建 node-sass 对我来说也没有意义。有什么建议么?

4

1 回答 1

1

对于遇到相同问题的任何人,正如 pesho在此处建议的那样,问题是由 nodejs 4.2.4 安装的 npm 2.x 在root模式下不运行脚本,导致这两个脚本失败:

npm WARN cannot run in wd node-sass@3.4.2 node scripts/install.js (wd=/srv/www/myapp/node_modules/node-sass-middleware/node_modules/node-sass)
npm WARN cannot run in wd node-sass@3.4.2 node scripts/build.js (wd=/srv/www/myapp/node_modules/node-sass-middleware/node_modules/node-sass)

可以使用 --unsafe-perm 标志运行 npm install,但正如标志所暗示的那样,它是不安全的。更好的方法是遵循 Starefossens最佳实践并创建另一个用户。

于 2016-01-18T16:25:08.303 回答