NPM 安装在 docker 容器中失败 - npm WARN tar ENOENT:没有这样的文件或目录

Posted

技术标签:

【中文标题】NPM 安装在 docker 容器中失败 - npm WARN tar ENOENT:没有这样的文件或目录【英文标题】:NPM Install failing in docker container - npm WARN tar ENOENT: no such file or directory 【发布时间】:2020-04-08 03:28:28 【问题描述】:

我正在尝试编译一个 Nativescript 应用程序作为我们 Teamcity 部署策略的一部分。

当我运行 NPM install 时,我在尝试查找文件时遇到 ENOENT 错误,如下所示:

  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/create.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignAllWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assign.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/curry.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignIn.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/curryN.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignAllWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignInAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignInAllWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/curryRight.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignIn.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/curryRightN.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignInAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignInWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignInAllWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/date.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assignWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assoc.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/debounce.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/assocPath.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignInWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/deburr.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/at.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assignWith.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/defaults.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assoc.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/attempt.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/defaultsAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/assocPath.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/browserify-117e4a8d/test/shared_symlink/shared/index.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/before.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/defaultsDeep.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/at.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/defaultsDeepAll.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-7722a2ea/fp/bind.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-b2787570/fp/attempt.js'
  npm WARN tar ENOENT: no such file or directory, open '/home/my_user/BuildAgent/work/my_application/node_modules/.staging/lodash-8fa77886/fp/defaultTo.js'

我在发出npm install之前尝试清理node_modules文件夹,~/.npmpackage-lock.json,在不同的容器中运行相同,在运行npm install之前清理teamcity中的结帐文件夹,更改结帐文件夹和所有我能想到的。我也跑过npm cache clear --force

我也尝试在本地机器上运行相同的步骤,使用相同的容器,并且编译工作正常。

似乎有一些与 npm 相关的脏数据或类似的东西导致了这种情况,但我不知道是什么原因。

这是正在使用的 dockerfile:

FROM ubuntu:18.04

USER root

ENV android_HOME=/android-sdk PATH=$PATH:/android-sdk/tools:/android-sdk/tools/bin:/android-sdk:/platform-tools

RUN apt-get update && apt-get install -y sudo lib32z1 lib32ncurses5 g++ unzip openjdk-8-jdk zsh-common curl gnupg2 git

RUN curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - && \
apt-get install -y nodejs && \
curl "https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip" -o /tmp/sdk.zip && \
mkdir -p /android-sdk && \
unzip -q /tmp/sdk.zip -d /android-sdk && \
mkdir -p /root/.android/ && touch /root/.android/repositories.cfg && \
rm -rf /tmp/* && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/lib/apt/*

RUN echo "export JAVA_OPTS=\"$JAVA_OPTS\"" >> /root/.bashrc && \
echo "export ANDROID_HOME=$ANDROID_HOME" >> /root/.bashrc && \
echo "export PATH=$PATH" >> /root/.bashrc

RUN yes | /android-sdk/tools/bin/sdkmanager --licenses && \
/android-sdk/tools/bin/sdkmanager "tools" "platform-tools" "platforms;android-28" "build-tools;28.0.3" "extras;google;m2repository" "extras;android;m2repository"

RUN yes | npm install nativescript@6.2.0 -g --unsafe-perm && \
tns extension install nativescript-cloud@1.19.2 && \
tns usage-reporting disable && \
tns error-reporting enable

RUN nativescript doctor

如何清理?

更新

我还注意到npm uninstall loadash 触发了相同的行为和多个 enoent 消息。

不太清楚为什么......

【问题讨论】:

尝试清理缓存npm cache clean --force 你是如何安装 NPM 的?它是作为 root 用户安装的吗? 忘了提及,但这一步也是部署的一部分。 @Manoj 是的,NPM 作为 root。我知道这不是最佳选择,但此编译一直在工作并停止,无需更改部署管道/命令。 是的@LalanaChamika 我做到了。到目前为止,我在 Teamcity 和 Jenkins 上多次遇到过这个问题。这似乎是主机和容器文件之间的权限问题,主机和容器的不同用户 ID 搞砸了。 【参考方案1】:

确保日志输出结束。

在我的情况下是:

npm ERR! code ENOENT
npm ERR! syscall spawn git
npm ERR! path git
npm ERR! errno -2
npm ERR! enoent Error while executing:
npm ERR! enoent undefined ls-remote -h -t ssh://git@github.com/bodymovin/lottie-api.git
npm ERR! enoent 
npm ERR! enoent 
npm ERR! enoent spawn git ENOENT
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

如果不是,请检查日志末尾的实际问题。 我在运行 docker 容器时遇到了这种情况,结果发现其中一个包正在使用 git 拉取,正如您在日志中看到的那样,因此 git 命令需要位于运行 npm 命令的用户的 PATH 中。

如果不是这样,日志的结尾可能会告诉您实际错误是什么。 如果不清楚,可能是 package-lock.json 文件在运行 npm install 时具有 package.json 生成的旧引用。 尝试删除/重命名它并运行 npm install,然后比较差异。

如果仍然无法解决,可能 package.json 的引用太旧而无法被 npm 解析。

您可能在 Linux 系统上查找的 npm 缓存通常位于 ~/.npm 或 ~/.npm-global 下,具体取决于您如何配置 npm/node。

如果这些都不能解决问题,请用完整的日志更新你的答案,以及你运行它的确切环境和条件。 如果这只发生在 lodash 上,请尝试看看如果将其作为依赖项删除会发生什么。 祝你好运

【讨论】:

【参考方案2】:

我有同样的问题,我解决这个问题的方法是我检查了我的工作目录 在我的 dockerfile 中。

Look at this

FROM node:12

#Create app directory
WORKDIR /user/projects/backend/E-commerce/node-ecommerce

#Install app dependencies
#A wildcard is used to ensure both packages.json AND package-lock.json are copied
#where available (npm@5+)
COPY package*.json ./

RUN npm install
#if you are building your code for production
#RUN npm ci --only=production

#Bundle app source
COPY . .

EXPOSE 4040
CMD [ "node", "server.js" ]

我认为 dockerfile 是您遇到问题的地方,并检查您的依赖项

【讨论】:

【参考方案3】:

我遇到了这个问题,因为设备上没有剩余空间,这个错误不知何故出现了

【讨论】:

【参考方案4】:

当您的 SSH 密钥未加载到您的 SSH 代理(可能未永久存储并且您已重新启动系统)时使用私有存储库作为依赖项时,npm i 可能会导致这些警告。

例如。这个在 package.json 中

"dependencies": 
    "dependency-name": "git+ssh://git@bitbucket.org:projectname/somerepo.git"

然后,在安装依赖项时,我收到了 ALL 我的部门的npm WARN tar ENOENT: no such file or directory 警告,直到我将 SSH 密钥重新添加到我的操作系统的 SSH 代理。

以下是我对 SSH 密钥的简要说明:

ssh-add -K ~/.ssh/bb_rsa # add an SSH key to your OS' SSH agent
ssh-add ~/.ssh/bb_rsa # add an SSH key to your OS' SSH agent
ssh-add -l bb_rsa # list the keys within your SSH agent

# .ssh/config file
 Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/bb_rsa

【讨论】:

以上是关于NPM 安装在 docker 容器中失败 - npm WARN tar ENOENT:没有这样的文件或目录的主要内容,如果未能解决你的问题,请参考以下文章

无法在 docker 容器中全局正确安装 npm 包

npm install 在 docker 的 jenkins 管道中失败

Docker 容器中的 Npm run serve

docker 中的 npm 更新失败

在 Rails Docker 容器中捆绑安装失败

npm:后安装未在 docker 中运行