gpt4 book ai didi

node.js - 在Docker容器中找不到依赖项

转载 作者:行者123 更新时间:2023-12-02 19:44:17 26 4
gpt4 key购买 nike

我正在尝试在Docker容器中为我的节点项目设置测试环境。我正在使用以下Dockerfile:

FROM node:14.4.0-alpine

RUN npm install -g ava

USER node
WORKDIR /home/node
COPY --chown=node package*.json ./
RUN npm install
ENV PATH=/home/node/node_modules/.bin:$PATH

WORKDIR /test

CMD [ "npm", "test" ]
我正在使用以下命令运行容器:
docker run -v `pwd`:/test -t <name>
现在,我收到此错误:
> goFit@1.0.0 test /test
> ava

(node:24) UnhandledPromiseRejectionWarning: Error: Could not resolve required module ’esm’
at /home/node/node_modules/ava/lib/api.js:27:10
at Array.map (<anonymous>)
at resolveModules (/home/node/node_modules/ava/lib/api.js:23:25)
at new Api (/home/node/node_modules/ava/lib/api.js:48:26)
at Object.exports.run (/home/node/node_modules/ava/lib/cli.js:386:14)
at Object.<anonymous> (/home/node/node_modules/ava/cli.js:10:23)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:24) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:24) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
如果我使用以下命令设置 NODE_PATH环境变量:
ENV NODE_PATH=/home/node/node_modules
然后错误更改为:
  Uncaught exception in tests/workout_factory.test.js

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ava' imported from /test/tests/workout_factory.test.js
Did you mean to import ava/index.js?

› Did you mean to import ava/index.js?
› packageResolve (internal/modules/esm/resolve.js:620:9)
› moduleResolve (internal/modules/esm/resolve.js:659:14)
› Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:752:11)
› Loader.resolve (internal/modules/esm/loader.js:97:40)
› Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
› ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:50:40)
› link (internal/modules/esm/module_job.js:49:36)
如果有帮助,我正在使用以下 package.json:
{
"name": "goFit",
"version": "1.0.0",
"description": "api web para entrenamiento personal",
"main": "index.js",
"directories": {
"doc": "docs"
},
"type": "module",
"scripts": {
"test": "ava"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ElenaMerelo/goFit.git"
},
"keywords": [
"workout",
"fitness",
"fit"
],
"author": "Elena Merelo Molina",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/ElenaMerelo/goFit/issues"
},
"homepage": "https://github.com/ElenaMerelo/goFit#readme",
"devDependencies": {
"ava": "^3.13.0",
"esm": "^3.2.25"
},
"ava": {
"files": [
"tests/**/*",
"!tests/exercises_data.js"
],
"cache": false,
"require": [
"esm"
]
},
"dependencies": {}
}
有谁知道为什么不起作用?

最佳答案

Dockerfile有几处错误:

  • 您需要全局安装所有dev依赖项。他们需要对所有用户可用
  • 您需要使用文件系统顶部的权限来设置/node_modules目录。这不是一件好事,但它与/test处于同一级别,它将停止寻找。这使得Dockerfile保持这种形状
  • RUN npm install -g ava esm && mkdir /node_modules && chown node /node_modules && chmod 755 /node_modules

    USER node
    COPY --chown=node package*.json ./
    RUN npm install
    ENV PATH=/home/node/node_modules/.bin:$PATH

    WORKDIR /test

    CMD [ "npm", "test" ]
    还请记住
  • 在生产环境中使用npm ci。 npm我仅应用于安装单个软件包。
  • 仍然可能不需要
  • PATH设置。
  • 完成后,将package*json删除为root
  • 关于node.js - 在Docker容器中找不到依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64581921/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com