Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asked to install Typescript when already installed when building Docker image

I am trying to build a docker image of a Next.js/React application which uses Typescript.

Typescript is installed, and I am able to run a build locally without docker.

However, as the docker image is building, I get to the following point:

Step 8/10 : RUN npm run build
 ---> Running in ee577c719739

> [email protected] build /app
> next build

Creating an optimized production build...
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install typescript by running:

        npm install --save-dev typescript

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: `next build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I have installed Typescript already. This is very confusing for me.

The Docker image I am using looks like this:

FROM gcr.io/companyX/companyX-node-base:12-alpine

# Copy in the project files
COPY . .

# Clean
USER root
RUN rm -fr node_modules

ENV NODE_ENV=production

COPY package*.json ./

RUN npm install && \
    npm cache clean --force

RUN npm run build

EXPOSE 3000

# Running the app
CMD [ "npm", "start" ]
like image 476
BURGERFLIPPER101 Avatar asked Jan 10 '20 13:01

BURGERFLIPPER101


People also ask

Does TypeScript need to be installed globally?

You will need to install the TypeScript compiler either globally or in your workspace to transpile TypeScript source code to JavaScript ( tsc HelloWorld. ts ). You can test your install by checking the version or help.

How do I install TypeScript globally?

You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.7). An alternative is to use npx when you have to run tsc for one-off occasions.

What happens if I don't agree to the terms of Docker desktop?

If you do not agree to the terms, the Docker Desktop application will close and you can no longer run Docker Desktop on your machine. You can choose to accept the terms at a later date by opening Docker Desktop. Docker Desktop starts after you accept the terms. From the Windows Start menu, select Settings > Apps > Apps & features.

How do I install Docker desktop on Windows?

After downloading Docker Desktop Installer.exe, run the following command in a terminal to install Docker Desktop: Start-Process '.\win\build\Docker Desktop Installer.exe' -Wait install --quiet: suppresses information output when running the installer

Why am I unable to run a typescript file?

"Error: You are attempting to run a TypeScript file, but do not have TypeScript installed. Ensure you have 'typescript' installed to enable TypeScript support."

Can I share containers and images created with Docker desktop?

Containers and images created with Docker Desktop are shared between all user accounts on machines where it is installed. This is because all Windows accounts use the same VM to build and run containers. Note that it is not possible to share containers and images between user accounts when using the Docker Desktop WSL 2 backend.


2 Answers

When you run (even outside Docker)

export NODE_ENV=production
npm install

it only installs the dependencies from your package.json and not the devDependencies. On the other hand, you probably need the devDependencies to get tools like typescript.

The good solution here is to use a multi-stage build. The first stage installs all of the dependencies; the second stage copies out only what’s needed to run the application.

FROM gcr.io/companyX/companyX-node-base:12-alpine AS build

# Copy in only the parts needed to install dependencies
# (This avoids rebuilds if the package.json hasn’t changed)
COPY package.json package.lock .

# Install dependencies (including dev dependencies)
RUN npm install

# Copy in the rest of the project
# (include node_modules in a .dockerignore file)
COPY . .

# Build the project
RUN npm run build

# Second stage: runtime
FROM gcr.io/companyX/companyX-node-base:12-alpine

ENV NODE_ENV=production

# Again get dependencies, but this time only install
# runtime dependencies
COPY package.json package.lock .
RUN npm install

# Get the built application from the first stage
COPY --from=build /app/dist dist

# Set runtime metadata
EXPOSE 3000
CMD [ "npm", "start" ]
# CMD ["node", "dist/index.js"]

Note that this approach isn’t compatible with setups that overwrite the application content with arbitrary content from the host, or that try to store libraries in Docker volumes. The final image is self-contained and doesn’t require any host content, and can be run as-is on other systems without separately copying the source code.

like image 58
David Maze Avatar answered Sep 17 '22 22:09

David Maze


I got the same error on deploying Next.js app on AWS Amplify and I didn't want to move typescript from devdependencies to dependencies.

My solution was to change the build setting from npm install
to npm install --dev typescript && npm install

This will remove the error.

NOTE: I'm not sure this works for docker though.

like image 40
byyoung Avatar answered Sep 18 '22 22:09

byyoung