Search code examples
node.jsdockersasslibsass

"Error: The `libsass` binding was not found..." in nodejs 4.2.4 lts but not 5.4.1 using docker


I am setting up a docker container using nodejs 4.2.4 with expressjs using sass.

My 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

The express.js project was generated using their generator

 express -css sass --hbs myapp

which generates a skeleton project with this 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"
  }
}

Running this on my mac with node 4.2.4 works fine, but running it within the docker container gives the following error:

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 [email protected]
npm info using [email protected]
npm info prestart [email protected]
npm info start [email protected]

> [email protected] 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)

However, if I extend the docker image from 5.4.1-wheezy it works fine. There seem to be quite some problems with libsass and node (here, here and here). I tried all of the suggestions in those links with some still remaining in the Dockerfile above, but none of them had any effect. In a way it also does not make sense to me that I would have to rebuild node-sass if I am coming from a clean node installation. Any suggestions?


Solution

  • For anyone running into the same problem, the issue, as pesho suggested here, was that npm 2.x which gets installed by nodejs 4.2.4 does not run scripts when in root mode, causing these two scripts to fail:

    npm WARN cannot run in wd [email protected] node scripts/install.js (wd=/srv/www/myapp/node_modules/node-sass-middleware/node_modules/node-sass)
    npm WARN cannot run in wd [email protected] node scripts/build.js (wd=/srv/www/myapp/node_modules/node-sass-middleware/node_modules/node-sass)
    

    One could have run npm install with the --unsafe-perm flag, but as the flag suggests, it's unsafe. A better way would be to follow Starefossens best practices and create another user.