我在Windows上使用Docker。
错误:
Step 4/5 : RUN npm install -g
---> Running in ec6582e10f69
+ lfg-auth@1.0.0
added 1 package in 0.437s
Removing intermediate container ec6582e10f69
---> f2b9a25a51a3
Step 5/5 : RUN npm run build-tsc
---> Running in 6321ac31e370
> lfg-auth@1.0.0 build-tsc /app
> tsc
/app/node_modules/.bin/tsc: line 1: XSym: not found
/app/node_modules/.bin/tsc: line 2: 0021: not found
/app/node_modules/.bin/tsc: line 3: 8cbd85238d8fbeb66a0afc1d79bcd880: not found
/app/node_modules/.bin/tsc: line 4: ../typescript/bin/tsc: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! lfg-auth@1.0.0 build-tsc: `tsc`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the lfg-auth@1.0.0 build-tsc script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-02-17T09_27_42_522Z-debug.log
ERROR: Service 'auth' failed to build: The command '/bin/sh -c npm run build-tsc' returned a non-zero code: 1
Dockerfile:
FROM node:8.15.0-alpine
ADD . /app/
WORKDIR /app
RUN npm install -g
RUN npm run build-tsc
package.json:
"scripts": {
...
"build-tsc": "tsc",
...
},
"dependencies": {
...
"typescript": "^3.3.3"
}
如果我尝试在docker容器的终端中运行相同的命令,则它会完美运行。如果我将docker-compose放在命令字段中,它也可以正常工作。
答案 0 :(得分:3)
当我将Windows react-scripts build
文件夹安装在Linux容器中时,我发现node_modules/
存在此问题。您很可能在Windows中运行npm install
,然后在构建时将node_modules/
复制到映像中,或者将本地项目文件夹安装为Docker卷。无论哪种方式,您都试图在Linux上运行Windows可执行文件,但它会崩溃。
从技术上讲,这是因为二进制文件/app/node_modules/.bin/tsc
的内容看起来像
XSym
0021
8cbd85238d8fbeb66a0afc1d79bcd880
../typescript/bin/tsc
通过将node_modules/
添加到.dockerignore
文件中,您应该能够解决构建时问题。
如果像我一样,稍后出于开发目的将本地项目目录安装在容器中,则仍然必须手动丢弃node_modules/
文件夹,然后从容器内部再次运行npm install
。