我正在尝试在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>
现在,我收到此错误:
> [email protected] 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有几处错误:
/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
。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句