我正在为 api 项目的节点/打字稿设置一个 dockerized 开发环境。目标是在 docker 中运行所有内容,并且没有在主机上安装任何已安装的节点、npm 或模块。这是为了将所有版本的节点和所有模块与其他项目隔离开来。
。/节点
docker run \
-it \
-p "8080:80" \
--rm \
-w "/app" \
-v "$(pwd):/app" \
"node:10" "$@"
./npm
#!/bin/sh
./node npm $@
./npx
#!/bin/sh
./node npx $@
./package.json
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx ts-lint --project src $@"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"@types/node": "^10.12.15",
"ts-lint": "^4.5.1",
"typescript": "^3.2.2"
}
}
[编辑]
我./npm install
用来构建我的 node_modules。node_modules 位于共享卷中,因此在移除容器后它会保留在主机上。这样我就不需要 Dockerfile 来构建图像。
[/编辑]
当我运行 lint 命令时,出现以下错误:
testapi$ ./npx ts-lint -i
10: Pulling from node
Digest: sha256:5af431757f84bf7878ff72447eb993fc37afcd975874fff13278157bf83661e6
Status: Image is up to date for docker-remote.registry.kroger.com/node:10
npx: installed 32 in 2.883s
Cannot find module 'typescript'
我认为这与模块分辨率有关,但我不确定。我看到人们在全球范围内安装打字稿,但这意味着我必须做一个 Dockerfile 而不是使用股票节点图像。我不介意将 Dockerfile 用于开发,但我认为应该有一种方法可以在不这样做的情况下完成这项工作。