Home > Enterprise >  SyntaxError: Unexpected token ':' when run migration
SyntaxError: Unexpected token ':' when run migration

Time:10-05

I have an enviments.ts file.

class Environment {
  get databaseName(): string {
    return ( 
      process.env.MODE === 'dev' ?
      process.env.dataBaseDev :
      process.env.dataBaseProd
    );
  }
}
export const Environments = new Environment();

and also migrate-mongo-config.js file in my root.

const Environments = require("./src/environment/environment.ts")

const config = {
  mongodb: {
    url: 'mongodbUrl',

    databaseName: Environments.databaseName,

    options: {
      useNewUrlParser: true, 
      useUnifiedTopology: true, 
    }
  },

  migrationsDir: "migrations",

  migrationFileExtension: ".js"
};

module.exports = config;

when I run my migration I have this error ERROR. also added when I write the database name as static it works but when I want to manage it dynamically I have this error

Unexpected token ':' /Users/x/Desktop/v/x-backend/src/environment/environment.ts:2
  get databaseName(): string {
              ^

SyntaxError: Unexpected token ':'
    

how can I solve this problem?

CodePudding user response:

You should require from dist (js):

const Environments = require("./dist/environment/environment")

CodePudding user response:

I believe the answer from Vahid Najafi is likely your problem candidate. Unfortunately his answer is very terse. For those starting to learn TypeScript, please allow me to elaborate on his answer.

When configuring your environment to use TypeScript you can enable a separation between TypeScript source code files and compiled output javascript code. The TypeScript compiler will read the source .ts files and output to .js files. It is generally recommended to keep these two collections of files in separate directories to allow building a deployment. To specify these different directories, you can modify your tsconfig.json file to specify the value for outDir and rootDir.

When you compile your .ts files using the command tsc the typescript files with type specific language will be converted to non-type-specific .js files. When considering your specific problem we see the file migrate-mongo-config.js refers to file ./src/environment/environment.ts. The file ./src/environment/environment.ts is a sourcecode file, not the compiled version. During runtime the program is expecting to refer to a javascript file, not a .ts file. So the corrective action is to modify the file migrate-mongo-config.js. Assuming you have configured your outDir to be ./dist the first line in the migrate-mongo-config.js should be altered.

Updated Version of migrate-mongo-config.js`

const Environments = require("./dist/environment/environment.js")

const config = {
  mongodb: {
    url: 'mongodbUrl',

    databaseName: Environments.databaseName,

    options: {
      useNewUrlParser: true, 
      useUnifiedTopology: true, 
    }
  },

  migrationsDir: "migrations",

  migrationFileExtension: ".js"
};

module.exports = config;

Notice how the first line value ./src/... was changed to ./dist/...? Notice how the file extension was changed from .ts to .js? Vahid's example removes the extension altogether. I have not tried this so I am uncertain as to it's behavior.

If you find this fixes your issue, please award the points to Vahid Najafi as this is his original idea. I am merely filling in some of the beginner topics to round out the conversation.

  • Related