我正在尝试在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:
答案 0 :(得分:0)
这可以归结为您的(本地).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。