我正在为 api 项目的 Node/ typescript 设置一个 dockerized 开发环境。目标是在 docker 中运行所有内容,并且没有在主机上安装任何已安装的 Node 、npm 或模块。这是为了将所有版本的 Node 和所有模块与其他项目隔离开来。
。/Node
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'
我认为这与模块分辨率有关,但我不确定。我看到人们在全局范围内安装 typescript ,但这意味着我必须做一个 Dockerfile 而不是使用股票 Node 图像。我不介意将 Dockerfile 用于开发,但我认为应该有一种方法可以在不这样做的情况下完成这项工作。
最佳答案
所以我想出了答案。这不是很明显,我偶然发现了它。
我已经安装了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 容器中运行时有效,只需使用公共(public) Node :10 图像。它不需要 Dockerfile 来安装任何全局依赖项。
关于javascript - npx ts-lint 在 docker 中运行时找不到模块 'typescript',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834565/