Sling Academy
Home/Node.js/Fixing Sequelize Error TS1086 in Node.js Projects

Fixing Sequelize Error TS1086 in Node.js Projects

Last updated: December 29, 2023

Understanding the Error

The ‘Sequelize Error TS1086: An accessor cannot be declared in an ambient context’ often occurs when using TypeScript with Sequelize in a Node.js project. This error generally indicates a version compatibility issue between TypeScript and Sequelize. TypeScript introduced more strict checks in version 3.7, which can trigger this error if Sequelize or its types have not been updated to work with these changes.

Solving the Error

To solve this issue, you need to ensure that the versions of TypeScript and Sequelize are compatible. Start by checking the version of TypeScript you are using. If you are on a version earlier than 3.7, you may need to update TypeScript to the latest version. You can update TypeScript using the npm package manager with the command npm install -g typescript@latest. Next, ensure your Sequelize and associated types are up to date by running npm install sequelize@latest @types/sequelize@latest.

If you prefer to stick with an older version of TypeScript, you must install a version of Sequelize that is compatible with it. You can find the appropriate version in the Sequelize release notes or by trying earlier releases of Sequelize and its types. To install a specific version of Sequelize, use npm install sequelize@[version] where [version] is the desired version number.

Updating Project Dependencies

After updating the packages, you should also update your projects dependencies in the package.json file. This file should reflect the new versions of TypeScript and Sequelize you have installed. Doing so ensures that anyone else working on the project or any deployment environments use the correct versions.

Code Example

Assuming you have corrected your versioning issues, you can test the Sequelize integration with TypeScript using a simple model definition. Below is an example of a Sequelize model for a ‘User’ in a Node.js application:

import { Model, DataTypes } from 'sequelize';
import sequelize from './database';

class User extends Model {
  public id!: number; // Note: the exclamation mark is TypeScript non-null assertion operator
  public name!: string;
  public email!: string;
}

User.init({
  id: {
    type: DataTypes.INTEGER.UNSIGNED,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    type: new DataTypes.STRING(128),
    allowNull: false,
  },
  email: {
    type: new DataTypes.STRING(128),
    allowNull: false,
  },
}, {
  tableName: 'users',
  sequelize: sequelize, // this is the 'sequelize' instance from 'database.ts'
});

export default User;

With this model, you should be able to perform database operations without encountering the TS1086 error, provided that your TypeScript and Sequelize versions are compatible.

Next Article: Sequelize.js: Select rows by group

Previous Article: How to use BCrypt with Sequelize models

Series: Sequelize.js Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates