moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 509 forks source link

Error with lodash dependency when running simple Mosca implementation #547

Closed brett--anderson closed 7 years ago

brett--anderson commented 7 years ago

Please see issue #546 first as it may be causing the issue documented here. I have a simple Mosca application. I'm running it inside a Dockerfile like this:

FROM mhart/alpine-node:4

RUN apk update && \
    apk add make gcc g++ python git krb5-dev

RUN mkdir -p /usr/src/app
RUN mkdir -p /app/db

WORKDIR /usr/src/app/

COPY ./ /usr/src/app/

RUN npm install --unsafe-perm --production

EXPOSE 80
EXPOSE 1883

ENTRYPOINT ["node","app.js"]

I'm building the image then using it with the command:

docker run -it -v $PWD:/usr/src/app --entrypoint=/bin/ash image_name

I have a package.json file that looks like this:

{
  "name": "test",
  "version": "0.1.0",
  "description": "MQTT broker as a module",
  "main": "app.js",
  "engines": {
    "node": ">= 0.12"
  },
  "dependencies": {
    "mosca": "^2.1.0"
  }
}

And app.js looks like this:

var mosca = require('mosca')

var backend = {
    type: "kafka",
    json: false,
    connectionString: "kafka01:2181,kafka02:2181:kafka03:2181",
    clientId: "mosca",
    groupId: "mosca",
    defaultEncoding: "utf8",
    encodings:{
          "spiddal-adcp": "buffer"
    }
};

var moscaSettings = {
    interfaces: [
        { type: "mqtt", port: 1883 }
    ],
    id: "mosca",

     /*
     * avoid publishing to $SYS topics because
     * it violates kafka topic naming convention
     */
    stats: false,
    publishNewClient: false,
    publishClientDisconnect: false,
    publishSubscriptions: false,

    logger: { name: 'MoscaServer', level: 'debug' },

    persistence: { factory: persistence.LevelUp, path: "/app/db" },

    backend: backend,
};

var server = new mosca.Server(moscaSettings); 
server.on('ready', setup);

function setup() {
  console.log('Mosca server is up and running')
}

server.on("error", function (err) {
    console.log(err);
});

When I execute node app.js in the container I get the following error:

module.js:327
    throw err;
    ^

Error: Cannot find module './lodash'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/usr/src/app/node_modules/mosca/node_modules/ascoltatori/node_modules/ioredis/node_modules/lodash/index.js:1:80)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)

I'd appreciate any help with this.

mcollina commented 7 years ago

Does this happen all the time? npm install reporta any error? The automated build of a Docker image works ok.

brett--anderson commented 7 years ago

Hi, thanks for responding :) It happens every time. npm installs everything, but there is one error thrown which I described in #546 . I've recreated the error using the files I described above on two different computers.

If you clone this repository and then build the Dockerfile, do you get the same error I'm seeing in #546 ?

mcollina commented 7 years ago

are you on windows? try adding a .dockerignore file in your folder with this content node_modules

brett--anderson commented 7 years ago

I'm on OSX. When I get the error described in #546 I'm using an exact clone of this repository, which includes the .dockerignore file. I tried adding the file to the simple container described above and now I'm getting a different error. I'll update this question after I investigate a little more.

brett--anderson commented 7 years ago

That seems to have solved the problem! Thanks so much for your help! If you have a moment, can you tell me why that .dockerignore file solved the problem, or point me in the direction of a blog that explains why?

mcollina commented 7 years ago

Something did not go well during npm install on your box, and you were uploading the bad dependencies when you were running docker build.