首页 > 解决方案 > 在 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:

标签: typescriptdockerdocker-composedockerfile

解决方案


这归结.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。


推荐阅读