Express.js在docker容器中找不到模块'opencv'

时间:2017-10-25 15:40:24

标签: javascript node.js opencv express npm

我正在尝试为NODE安装OpenCV绑定,以便我可以在我的快速服务器上使用AI。我正在使用Peter Braden图书馆 - https://github.com/peterbraden/node-opencv

但是,在旋转Docker容器时,我一直遇到一个重复的问题:

express_1  | Error: Cannot find module 'opencv'
express_1  |     at Function.Module._resolveFilename (module.js:485:15)
express_1  |     at Function.Module._load (module.js:437:25)
express_1  |     at Module.require (module.js:513:17)
express_1  |     at require (internal/module.js:11:18)
express_1  |     at Object.<anonymous> (/myapp/routes/OpenCV.js:3:10)
express_1  |     at Module._compile (module.js:569:30)
express_1  |     at Object.Module._extensions..js (module.js:580:10)
express_1  |     at Module.load (module.js:503:32)
express_1  |     at tryModuleLoad (module.js:466:12)
express_1  |     at Function.Module._load (module.js:458:3)
express_1  |     at Module.require (module.js:513:17)
express_1  |     at require (internal/module.js:11:18)
express_1  |     at Object.<anonymous> (/myapp/app.js:69:16)
express_1  |     at Module._compile (module.js:569:30)
express_1  |     at Object.Module._extensions..js (module.js:580:10)
express_1  |     at Module.load (module.js:503:32)
express_1  | npm info lifecycle backend@0.0.0~start: Failed to exec     start script
express_1  | npm ERR! code ELIFECYCLE
express_1  | npm ERR! errno 1
express_1  | npm ERR! backend@0.0.0 start: `node ./bin/www`
express_1  | npm ERR! Exit status 1
express_1  | npm ERR! 
express_1  | npm ERR! Failed at the backend@0.0.0 start script.
express_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
express_1  | 
express_1  | npm ERR! A complete log of this run can be found in:
express_1  | npm ERR!     /root/.npm/_logs/2017-10-25T15_32_12_258Z-debug.log
maya_express_1 exited with code 1

我试过访问容器并使用bash来安装open cv但这也不起作用。

我在Sierra 10.12.6上运行

如果删除node_modules然后运行 ​​-

docker run -v $(pwd):/myapp express-dev:1.0.0 npm install
它给我一个完全不同的错误

 gyp info spawn args   '-Goutput_dir=.' ]
 /myapp/node_modules/opencv/utils/find-opencv.js:21
                 throw new Error("ERROR: failed to run: pkg-config", opencv, flag);
            ^

 Error: ERROR: failed to run: pkg-config
     at /myapp/node_modules/opencv/utils/find-opencv.js:21:23
     at ChildProcess.exithandler (child_process.js:277:5)
     at emitTwo (events.js:125:13)
     at ChildProcess.emit (events.js:213:7)
     at maybeClose (internal/child_process.js:921:16)
     at Socket.stream.socket.on (internal/child_process.js:348:11)
     at emitOne (events.js:115:13)
     at Socket.emit (events.js:210:7)
     at Pipe._handle.close [as _onclose] (net.js:549:12)
 gyp: Call to 'node utils/find-opencv.js --cflags' returned exit status 1 while in binding.gyp. while trying to load binding.gyp

我尝试过重新连接opencv并使用opencv @ 2,但没有运气。

我的Dockerfile:

# Based off: https://hub.docker.com/r/danlynn/ember-cli/
# See ./provisioning/containers for more.

FROM node:8.2.1
MAINTAINER thestartupfactory.tech <hello@thestartupfactory.tech>
ENV WORKDIR=/opt NODE_ENV=production

# express server on 3000
# ember will be compiled and served via express
EXPOSE 3000
# Use /opt for building container
WORKDIR $WORKDIR

# See WORKDIR command at end of file
CMD ["npm", "start"]

# Update the image
# Install ember dev dependencies
# Install ember-cli and express-generator
RUN \
    apt-get update -y && \
    npm install -g bower@1.8.0 &&\
    npm install -g ember-cli@2.14.2 &&\
    npm install -g express-generator@4.15.0

# Import the code
RUN \
    mkdir -p /opt/ember && \
    mkdir -p /opt/express

COPY ./frontend/ /opt/ember/
COPY ./backend/ /opt/express/

# Run npm install
# Compile ember, insert to express && delete ember source
# Cleanup caches
RUN \
    cd /opt/ember && npm install &&\
    ember build --environment production && mv dist/ ../express/ &&\
    cd .. && rm -rf ember &&\
    cd /opt/express && npm install && cd .. &&\
    apt-get clean &&\
    apt-get autoremove --purge

# Move the WORKDIR ready for entrypoint
WORKDIR $WORKDIR/express

任何帮助将不胜感激! 提前致谢

1 个答案:

答案 0 :(得分:2)

看起来你甚至没有在docker镜像中设置node-opencv。在本地计算机上和docker镜像中设置node-opencv之间存在差异。在Dockerfile中,您必须包含用于设置opencv和node-opencv的命令。如果您不确定如何在Linux环境中设置OpenCV,可以参考OpenCV travis.yml。

或者您可以查看opencv-express。这是如何将opencv与nodejs和express一起使用的一个例子。但它使用opencv4nodejs。您也可以只提取其中一个提供的基本映像,具体取决于您要使用的OpenCV版本,并在node-opencv的顶部安装节点环境。