Error: Import statement is not permitted outside a module in TypeScript when using next.js

While developing the backend of my app with Nest.js and TypeScript, I encountered an issue when attempting to build the project using docker-compose. The error message displayed was as follows:

    /app/src/users/users.entity.ts:1
backend_1   | import { Entity, PrimaryGeneratedColumn, Column, BeforeInsert } from 'typeorm';
backend_1   | ^^^^^^
backend_1   | 
backend_1   | SyntaxError: Cannot use import statement outside a module
backend_1   |     at internalCompileFunction (node:internal/vm:76:18)
...

I also provided my tsconfig file for reference:

  {
  "compilerOptions": {
    ...
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

In case it helps, here is the Dockerfile I am using:

FROM node:18

# Install wait-for-it
ADD https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh /usr/bin/wait-for-it
RUN chmod +x /usr/bin/wait-for-it
...

Although I came across a solution for a similar problem that involves an 'ormconfig' file, I couldn't locate any such file in my project directory.

Answer №1

To achieve this, you might want to consider avoiding the use of NPM for project execution in the starting phase.

Instead, ensure that you include RUN npm run build in your Dockerfile to compile the project, followed by using CMD ["dist/main.js"] to execute the compiled bundle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating Elements in an Array Using JavaScript is Not Functioning as Expected

In my Angular application, I have included some lines of TypeScript code which involve Boolean variables in the constructor and an array of objects. Each object in this array contains input variables. selftest: boolean; failed: boolean; locoStateItem ...

Is it possible to filter out certain fields from TypeORM relations or selectively include specific fields?

Within my TypeORM setup, I have established OneToMany and ManyToOne relationships that are functioning correctly. In my find function, I am able to specify certain columns from the original table using the 'select' statement like this: return a ...

Mapped TypeScript type requiring scalar properties and allowing optional objects

I am in need of a TypeScript generic type that has the capability to alter another type so that all scalar properties (such as strings, numbers, booleans, etc.) remain mandatory, while object types become optional. For instance, given the User type below, ...

Is there a way for me to access the request.user object within a middleware when the user is obtained from the JwtAuthGuard?

I'm in the process of developing a middleware to retrieve a member entity and attach it to the request object. However, I require the userId from the user object obtained through JwtAuthGuard, and unfortunately the guards are executed after middleware ...

Is there a way to prevent IntelliJ from creating .js files when working with .ts source code?

Working on a mixed Java/Typescript project with Maven as the build tool, I utilize the frontend-maven-plugin to successfully build from the command line. However, I am encountering an issue with IntelliJ 2018.2 where it keeps transpiling .js files for my . ...

When you utilize useSelector, the state may be returned as undefined even after it has been initialized

In the process of developing a project that mirrors Twitter(X), my approach involves implementing state management with REDUX and maintaining persistent login using redux-persist. I am storing the user's username and token in both the Redux store (for ...

Angular: proper dependency injection may not occur when appending .js or .ts at the end of an import statement

When it comes to import statements, the format is usually as follows: import {HelpService} from '../../help.service' It's worth noting that if I utilize autowiring to inject HelpService into the constructor, an already existing instance of ...

Bring in modules from around the world into your Ionic framework built with Angular

While working with the ionic framework, I have noticed that many of my pages require the same modules. import { Http } from '@angular/http'; import { Storage } from '@ionic/storage'; Therefore, I find myself setting them in the constr ...

Tips on creating a literal type that is determined by a specific value within an object

In the flow, I am able to create a dynamic literal type in this manner: const myVar = 'foo' type X = { [typeof myVar]: string } const myX: X = { foo: 1 } // will throw, because number const myX: X = { foo: 'bar' } // will not throw ...

Ways to modify Angular pipe without specifying data types

I am in the process of extracting an Angular pipe from an application to a library. The current pipe is tied to specific types, but I want to change it to use generic types while maintaining type safety. Original Pipe: import { Pipe, PipeTransform } fr ...

Error message during ng Build Prod: Component declared in two modules when it should be in the same module

When running the ng build --prod command, I encountered an error message that is causing some confusion: The error states: Type SiteSecurity in "PATH"/siteSecurity.component.ts belongs to the declarations of 2 modules: SiteSecurityModule in "PATH"/s ...

Is it possible for NodeJS to prioritize IPv6 DNS lookups as the default option?

In my work with multiple TypeScript (NodeJS 14) client applications that are all Dockerized, I primarily use axios for most HTTP requests. However, there are other tools used as well. Typically, DNS queries default to resolving IPv4 addresses, resulting i ...

The 'authorization' property is not available on the 'Request' object

Here is a code snippet to consider: setContext(async (req, { headers }) => { const token = await getToken(config.resources.gatewayApi.scopes) const completeHeader = { headers: { ...headers, authorization: token ...

Tips on storing and retrieving data between pages in Ionic 4/5: Saving data to a service and accessing it from a

Looking for assistance from the community I am trying to pass data between pages using a service in Angular. Here is the code for the parent component.ts file: import { Component } from '@angular/core'; import { ShareService } from '../sh ...

What are the steps to validate a form control in Angular 13?

My Angular 13 application has a reactive form set up as follows: https://i.sstatic.net/LE219.png I am trying to validate this form using the following approach: https://i.sstatic.net/gxpgN.png However, I encountered the following error messages: https:// ...

Employing TypeScript decorators to enforce a required parameter in functions

Working on a typescript project, I encountered over 2000 events(methods) that are triggered during user operations. To ensure performance metrics for all events related to the completion of an operation, I decided to make 'executionTime' a mandat ...

What is the purpose of declaring a variable within a class and then assigning it to "this" in the constructor?

The following code snippet demonstrates the declaration of a variable constObj within the Test class. The object constObj is then assigned to this in the constructor. Why must we declare the variable again when it is already being assigned to this in the ...

Guide on deactivating the div in angular using ngClass based on a boolean value

displayData = [ { status: 'CLOSED', ack: false }, { status: 'ESCALATED', ack: false }, { status: 'ACK', ack: false }, { status: 'ACK', ack: true }, { status: 'NEW', ack ...

javascript identify dissimilarities within arrays

Working on an Angular 2 application and attempting to identify the difference between two arrays (last seven days and missing dates within the last seven days). Everything works fine when initializing the array through a string, like in example code 1. How ...

Nginx will initiate the React Redux state when there is a change in the route

Creating an application using React for the front end, Postgres as the database, Express.js for API, and Nginx for routing management. The React app utilizes React-Router and Redux for state management. Encountered a problem where the Redux state was reset ...