Npm安装速度慢,几乎每个库都出现“缓存未命中”。

huangapple go评论110阅读模式
英文:

Npm install being slow and lots of "cache miss" after nearly every library

问题

以下是您要翻译的内容:

"While everything seems to work on my local computer;

Whenever I run nodejs inside a docker (docker run node:18) and I clone a project, type npm install to get all libraries and work with them, it is really slow. Like 10 seconds slow.

While it works quickly after this initial bump I also notice that each library (as far as I can tell) has a (cache miss) appended after the timing (which is around 10 seconds). What is happening is this a problem/can I fix it?

Just to stress: it happens in any docker, whether I use node-alpine, node docker or even just an ubuntu docker and install node there manually.


After some hints from @NaorTedgi I notice that this is indeed due to the fact that the package is situated outside the docker and linked through a volume. I also notice that the timing itself (the 15 seconds) is dependent on the amount of packages it tries to load. With a single package it's too fast too notice and with a few it's only half a second.

So to test it the following steps I take:

make a new directory (~/javascript-test) and put the following into a package.json file:

{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "NODE_ENV=production node ./javascript/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"connect-redis": "^6.1.3",
"cookie-parser": "^1.4.6",
"date-fns": "^2.28.0",
"debug": "^4.3.3",
"express": "^4.17.2",
"express-session": "^1.17.2",
"http-errors": "^2.0.0",
"knex": "^2.1.0",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nanoid": "^3.3.4",
"node-cron": "^3.0.1",
"objection": "^3.0.1",
"pg": "^8.7.1",
"redis": "^4.2.0",
"typescript": "^4.7.4",
"uuid": "^8.3.2"
}
}

Open shell to this directory and run (to initialize package.json)

npm install
rm -rf node_modules

Then run the docker with the volume (obviously with sudo if required):

docker run --name node-test --rm -it -v ~/javascript-test:/javascript node:18

Open a second shell (since the default entrypoint isn't sh from the node dockers) and execute:

docker exec -it node-test sh

Inside the docker shell:

cd javascript && npm install

With these steps I notice cache misses after 2 seconds.

Finally I notice that if I do remove node_modules and reinstall the modules (npm install) a second time inside the docker no cache misses happen. So to test a second time one has to end the node docker and rerun it (docker run... in the first shell).


For those who like a dockerfile, this is the simplest file that still exhibits the error (once again make sure to bind the volume containing above package.json and the corresponding package-lock.json)

FROM node:18
WORKDIR /javascript
ENTRYPOINT npm install

A git repository of the Dockerfile and the package.json/package-lock.json files: https://github.com/pulli23/docker-npm-test

run it through (if cloned into ~/dockertest)

sudo docker build -t nodetest . && sudo docker run --name node-test --rm -it -v ~/dockertest/javascript-test:/javascript nodetest

希望这有所帮助。

英文:

While everything seems to work on my local computer;

Whenever I run nodejs inside a docker (docker run node:18) and I clone a project, type npm install to get all libraries and work with them, it is really slow. Like 10 seconds slow.

While it works quickly after this initial bump I also notice that each library (as far as I can tell) has a (cache miss) appended after the timing (which is around 10 seconds). What is happening is this a problem/can I fix it?

Just to stress: it happens in any docker, whether I use node-alpine, node docker or even just an ubuntu docker and install node there manually.

<hr>

After some hints from @NaorTedgi I notice that this is indeed due to the fact that the package is situated outside the docker and linked through a volume. I also notice that the timing itself (the 15 seconds) is dependent on the amount of packages it tries to load. With a single package it's too fast too notice and with a few it's only half a second.

So to test it the following steps I take:

make a new directory (~/javascript-test) and put the following into a package.json file:

{
  &quot;name&quot;: &quot;backend&quot;,
  &quot;version&quot;: &quot;1.0.0&quot;,
  &quot;description&quot;: &quot;&quot;,
  &quot;main&quot;: &quot;app.js&quot;,
  &quot;scripts&quot;: {
    &quot;start&quot;: &quot;NODE_ENV=production node ./javascript/app.js&quot;
  },
  &quot;keywords&quot;: [],
  &quot;author&quot;: &quot;&quot;,
  &quot;license&quot;: &quot;ISC&quot;,
  &quot;dependencies&quot;: {
    &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
    &quot;connect-redis&quot;: &quot;^6.1.3&quot;,
    &quot;cookie-parser&quot;: &quot;^1.4.6&quot;,
    &quot;date-fns&quot;: &quot;^2.28.0&quot;,
    &quot;debug&quot;: &quot;^4.3.3&quot;,
    &quot;express&quot;: &quot;^4.17.2&quot;,
    &quot;express-session&quot;: &quot;^1.17.2&quot;,
    &quot;http-errors&quot;: &quot;^2.0.0&quot;,
    &quot;knex&quot;: &quot;^2.1.0&quot;,
    &quot;morgan&quot;: &quot;^1.10.0&quot;,
    &quot;multer&quot;: &quot;^1.4.5-lts.1&quot;,
    &quot;nanoid&quot;: &quot;^3.3.4&quot;,
    &quot;node-cron&quot;: &quot;^3.0.1&quot;,
    &quot;objection&quot;: &quot;^3.0.1&quot;,
    &quot;pg&quot;: &quot;^8.7.1&quot;,
    &quot;redis&quot;: &quot;^4.2.0&quot;,
    &quot;typescript&quot;: &quot;^4.7.4&quot;,
    &quot;uuid&quot;: &quot;^8.3.2&quot;
  }
}

Open shell to this directory and run (to initialize package.json)

npm install
rm -rf node_modules

Then run the docker with the volume (obviously with sudo if required):

docker run --name node-test --rm -it -v ~/javascript-test:/javascript node:18

Open a second shell (since the default entrypoint isn't sh from the node dockers) and execute:

docker exec -it node-test sh

Inside the docker shell:

cd javascript &amp;&amp; npm install

With these steps I notice cache misses after 2 seconds.

Finally I notice that if I do remove node_modules and reinstall the modules (npm install) a second time inside the docker no cache misses happen. So to test a second time one has to end the node docker and rerun it (docker run... in first shell).

<hr>
For those who like a dockerfile, this is the simplest file that still exhibit the error (once again make sure to bind the volume containing above package.json and the corresponding package-lock.json)

FROM node:18
WORKDIR /javascript
ENTRYPOINT npm install

A git repository of the Dockerfile and the package.json/package-lock.json files: https://github.com/pulli23/docker-npm-test

run it through (if cloned into ~/dockertest)

sudo docker build -t nodetest .  &amp;&amp; sudo docker run --name node-test --rm -it -v ~/dockertest/javascript-test:/javascript nodetest

答案1

得分: 3

  1. 如果您使用多个注册表,请确保npm是您的默认注册表。

npm config set registry=https://registry.npmjs.com/

  1. 如果您的项目仓库有package-lock.json文件:
  • 确保您没有将node_modules目录的卷积映射到您的镜像中,如果是这样的话,在安装之前删除node_modules将会更快。
  • 使用ci运行安装 => npm ci
  1. 确保您的package-lock.json是从容器内部运行的npm i命令生成的!在本地运行npm i将生成与您本地Node版本兼容的库,对于C++附加组件,还会生成适用于您的操作系统的dll、so或dylib文件。
英文:

few things you need to check:

  1. if your using more then one registry make sure npm is your default

npm config set registry=https://registry.npmjs.com/

  1. if your project repository has package-lock.json
  • then make sure your not using volume to the current directory node_modules dir to your image if so it will be much faster to delete node_modules before installation
  • run install with ci => npm ci
  1. make sure your package-lock.json is generated from the ‘npm i’ command from inside the container! Runing npm i locally will genreate libraries competable to your local node version and in case of c++ add ons genreate dll,so or dylib file according to your OS

答案2

得分: 2

我猜这可能与网络(如代理)或身份验证(如连接到企业存储库)有关。您的本地连接可能具有容器缺少的某些内容,也许是某个证书。

您应该进入容器并测试是否可以访问存储库。

英文:

My guess it is an issue related to networking (f.e. proxy) or authentication (f.e. connection to enterprise repository). Your local connection has something what your container is missing, maybe some certificate.

You should go into the container and test if you can reach the repository.

huangapple
  • 本文由 发表于 2023年1月5日 19:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75017932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定