0

我正在为 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 用于开发,但我认为应该有一种方法可以在不这样做的情况下完成这项工作。

4

2 回答 2

2

所以我想出了答案。这不是很明显,我偶然发现了它。

我已经安装了ts-lint(参见上面的 package.json),我看到了一个引用的示例tslint(没有连字符)。

所以我删除ts-lint并安装了tslint它,它就像一个冠军。我不确定有什么区别,但是带有连字符的那个在我的项目配置中不起作用。此外,没有连字符的版本号比带有连字符的版本号更高。

查看包含工作依赖项的新 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 tslint --project ./ 'src/**/*.ts?(x)' $@"
  },
  "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",
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  }
}

这在 docker 容器中运行时有效,只需使用公共节点:10 图像。它不需要 Dockerfile 来安装任何全局依赖项。

于 2018-12-19T13:24:59.747 回答
0

尝试

yarn global add tslint typescript

或者如果它抱怨权限:

sudo yarn global add tslint typescript
于 2020-08-03T00:48:43.533 回答