You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在import语句中使用__dirname统一导入DB模型文件?

Using __dirname for Consistent Model Imports in Sequelize

Great question! Using __dirname to standardize imports across different file paths is totally possible, but the error you're seeing is almost certainly tied to whether your project uses CommonJS (CJS) or ES Modules (ESM), or a simple path-syntax mistake. Let's break this down step by step:

First: Why Your __dirname Attempt Might Be Failing

The most common issues are:

  • You're working in an ESM environment (where __dirname isn't a built-in variable by default)
  • You messed up relative path segments (like missing ../ to traverse up directories)
  • You forgot to use the path module to safely join paths (avoids cross-platform issues)

Solution 1: For CommonJS Projects (require/module.exports)

If your project uses CommonJS (the default for Node.js before v14, or if your package.json doesn't have "type": "module"), __dirname is available out of the box. Use the path module to construct absolute paths reliably:

Example 1: src/index.js (same root as the models directory's parent)

const path = require('path');
const UserModel = require(path.join(__dirname, 'sequelize', 'models', 'User'));

Example 2: src/facebook/index.js (one level deeper than src/)

const path = require('path');
// Use '../' to go up one directory to src/, then navigate to models
const UserModel = require(path.join(__dirname, '..', 'sequelize', 'models', 'User'));

Pro tip: Add a console.log(path.join(__dirname, '..', 'sequelize', 'models', 'User')) to verify the absolute path matches your actual file location.


Solution 2: For ES Modules Projects (import/export)

If your package.json has "type": "module", __dirname isn't defined by default. You'll need to manually create it using Node's url and path modules:

Step 1: Define __dirname in your file

import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

Step 2: Use it to import your model

// For src/api/resolvers/index.js (two levels deeper than src/)
import UserModel from join(__dirname, '../..', 'sequelize', 'models', 'User.js');

Alternatively, you can skip defining __dirname and use import.meta.url directly:

import { normalize } from 'path';
import UserModel from normalize(new URL('../../sequelize/models/User.js', import.meta.url).pathname);

(The normalize fixes Windows-specific path issues with URL-generated paths.)


Even Better: Use Module Aliases (Avoid __dirname Altogether)

While __dirname works, using module aliases is a cleaner, more maintainable solution. It lets you import models with a consistent alias regardless of the file's location.

Example: Configure Aliases in package.json (Node.js >=14)

Add an exports field to your package.json:

{
  "exports": {
    "#models/*": "./src/sequelize/models/*"
  }
}

Now you can import models anywhere like this:

// CommonJS
const UserModel = require('#models/User');
// ES Modules
import UserModel from '#models/User';

Example: Webpack/Vite Alias

If you're using a bundler like Webpack or Vite, set up an alias in your config:

// webpack.config.js
module.exports = {
  resolve: {
    alias: {
      '@models': path.resolve(__dirname, 'src/sequelize/models')
    }
  }
};

Then import with:

import UserModel from '@models/User';

Common Error Fixes

  • ReferenceError: __dirname is not defined: You're in ESM mode. Follow the ESM solution above to define __dirname.
  • Cannot find module: Double-check your path segments (did you add enough ../?) and use console.log to verify the absolute path.
  • ESM import errors: Don't forget file extensions (e.g., User.js instead of User) in ESM imports.

内容的提问来源于stack exchange,提问作者Gijo Varghese

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:50:10