typescript - 在 docker 中为 express 应用程序编译打字稿时出错
问题描述
我正在尝试在 docker 中运行打字稿应用程序,但遇到了一些问题。
sh: 1: tsc: not found
尽管打字稿在开发依赖项中,但我收到的错误是。我理想地认为,在我的 dockerfile 中,我正在运行 npm install,它还会安装 devDependencies 并引入 Typescript,但这不是真的吗?
我package.json
的是:
{
"name": "ts-express-starter-kit",
"version": "1.0.0",
"description": "Opinionated starter kit for typescript and express",
"main": "dist/index.js",
"scripts": {
"dev": "concurrently -k -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold\" \"npm run watch-ts\" \"nodemon ./dist/index.js\"",
"watch-ts": "tsc -w"
},
"keywords": [
"typescript",
"express"
],
"dependencies": {
"@types/config": "0.0.34",
"@types/cookie-parser": "^1.4.1",
"@types/cors": "^2.8.5",
"@types/express": "^4.17.0",
"@types/lodash": "^4.14.138",
"@types/morgan": "^1.7.35",
"@types/mysql": "^2.15.6",
"@types/node": "^12.0.10",
"config": "^3.1.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-enrouten": "^1.3.0",
"lodash": "^4.17.11",
"moment": "^2.24.0",
"morgan": "^1.9.1",
"mysql": "^2.17.1",
"sequelize": "^5.18.4",
"sequelize-cli": "^5.5.0",
"typescript": "^3.5.1",
"winston": "^3.2.1"
},
"devDependencies": {
"concurrently": "^5.0.0",
"nodemon": "^2.0.1",
"ts-node": "^8.5.4"
}
}
我Dockerfile
的是:
FROM node:10
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "npm", "run", "dev" ]
并且docker-compose.yaml
是:
version: "3"
services:
app:
container_name: tw-backend # How the container will appear when listing containers from the CLI
build: .
image: node:10
user: node
working_dir: "/app"
networks:
- app
ports:
- "8080:8080"
volumes:
- ./:/app docker
networks:
app:
解决方案
这归结.bin
为您的(本地)node_modules
目录的目录未添加到环境$PATH
变量中,如下所示:
root@f932304632f2:/app# export
declare -x HOME="/root"
declare -x HOSTNAME="f932304632f2"
declare -x NODE_VERSION="10.17.0"
declare -x OLDPWD
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
declare -x PWD="/app"
declare -x SHLVL="1"
declare -x TERM="xterm"
declare -x YARN_VERSION="1.19.1"
列出/app/node_modules/.bin
(如您所见,毕竟已经安装了东西):
root@f932304632f2:/app# ls -la node_modules/.bin
total 16
drwxr-xr-x 2 root root 4096 Dec 1 08:40 .
drwxr-xr-x 308 root root 12288 Dec 1 08:40 ..
lrwxrwxrwx 1 root root 35 Dec 1 08:40 concurrently -> ../concurrently/bin/concurrently.js
lrwxrwxrwx 1 root root 37 Dec 1 08:40 css-beautify -> ../js-beautify/js/bin/css-beautify.js
lrwxrwxrwx 1 root root 32 Dec 1 08:40 editorconfig -> ../editorconfig/bin/editorconfig
lrwxrwxrwx 1 root root 38 Dec 1 08:40 html-beautify -> ../js-beautify/js/bin/html-beautify.js
lrwxrwxrwx 1 root root 15 Dec 1 08:40 is-ci -> ../is-ci/bin.js
lrwxrwxrwx 1 root root 36 Dec 1 08:40 js-beautify -> ../js-beautify/js/bin/js-beautify.js
lrwxrwxrwx 1 root root 19 Dec 1 08:40 json5 -> ../json5/lib/cli.js
lrwxrwxrwx 1 root root 14 Dec 1 08:40 mime -> ../mime/cli.js
lrwxrwxrwx 1 root root 20 Dec 1 08:40 mkdirp -> ../mkdirp/bin/cmd.js
lrwxrwxrwx 1 root root 25 Dec 1 08:40 nodemon -> ../nodemon/bin/nodemon.js
lrwxrwxrwx 1 root root 25 Dec 1 08:40 nodetouch -> ../touch/bin/nodetouch.js
lrwxrwxrwx 1 root root 19 Dec 1 08:40 nopt -> ../nopt/bin/nopt.js
lrwxrwxrwx 1 root root 12 Dec 1 08:40 rc -> ../rc/cli.js
lrwxrwxrwx 1 root root 23 Dec 1 08:40 semver -> ../semver/bin/semver.js
lrwxrwxrwx 1 root root 30 Dec 1 08:40 sequelize -> ../sequelize-cli/lib/sequelize
lrwxrwxrwx 1 root root 30 Dec 1 08:40 sequelize-cli -> ../sequelize-cli/lib/sequelize
lrwxrwxrwx 1 root root 19 Dec 1 08:40 tree-kill -> ../tree-kill/cli.js
lrwxrwxrwx 1 root root 22 Dec 1 08:40 ts-node -> ../ts-node/dist/bin.js
lrwxrwxrwx 1 root root 25 Dec 1 08:40 ts-script -> ../ts-node/dist/script.js
lrwxrwxrwx 1 root root 21 Dec 1 08:40 tsc -> ../typescript/bin/tsc
lrwxrwxrwx 1 root root 26 Dec 1 08:40 tsserver -> ../typescript/bin/tsserver
lrwxrwxrwx 1 root root 16 Dec 1 08:40 uuid -> ../uuid/bin/uuid
lrwxrwxrwx 1 root root 18 Dec 1 08:40 which -> ../which/bin/which
root@f932304632f2:/app#
你有几个选项来处理这个..你(应该)使用npx ..command..
(即:)npx tsc -w
并让节点为你处理分辨率或npm install typescript -g
(不推荐)或使用./node_modules/.bin/tsc -w
(好的,但如果你移动东西不安全)。我会选择#1。
推荐阅读
- python - 有没有比循环遍历数据框更好的方法将数据框转换为“真值表”?
- python - 集群 id 列中缺少值的问题
- mongodb - 使用 docker-entrypoint-initdb.d 创建的 Mongodb 用户不会保留在数据库中
- javascript - 从具有变量的对象中获取值反应
- javascript - 优化我的算法以检测需要更新的 SQL 记录
- python - 具有特定模式的元组列表
- django - 使用模型管理器不起作用,但在视图中使用过滤器进行查询可以,我哪里出错了?
- c - WHILE 循环和语句的 YACC 语法不起作用
- spring - 单例过滤器错误中的 Spring RequestScope 服务
- visual-studio - 复制 Visual Studio 的文件扩展名