英文:
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:
{
"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 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 . && sudo docker run --name node-test --rm -it -v ~/dockertest/javascript-test:/javascript nodetest
答案1
得分: 3
- 如果您使用多个注册表,请确保
npm
是您的默认注册表。
npm config set registry=https://registry.npmjs.com/
- 如果您的项目仓库有
package-lock.json
文件:
- 确保您没有将
node_modules
目录的卷积映射到您的镜像中,如果是这样的话,在安装之前删除node_modules
将会更快。 - 使用
ci
运行安装 =>npm ci
- 确保您的
package-lock.json
是从容器内部运行的npm i
命令生成的!在本地运行npm i
将生成与您本地Node版本兼容的库,对于C++附加组件,还会生成适用于您的操作系统的dll、so或dylib文件。
英文:
few things you need to check:
- if your using more then one registry make sure
npm
is your default
npm config set registry=https://registry.npmjs.com/
- 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
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论