我正在使用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"
}
}
在我的mac上使用节点4.2.4运行它可以正常工作,但在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和节点(here,here和here)似乎存在一些问题。我尝试了这些链接中的所有建议,其中一些仍然保留在上面的Dockerfile中,但它们都没有任何效果。在某种程度上,如果我来自干净的节点安装,我将不得不重建node-sass。有什么建议吗?
答案 0 :(得分:1)
对于遇到同样问题的人来说,问题就像pesho建议here那样,由nodejs 4.2.4安装的npm 2.x在 root mode,导致这两个脚本失败:
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 best practices并创建另一个用户。