Skip to content

Dynamic require failed #134

Open
Open
@Cupcc

Description

@Cupcc

What version of pkg are you using?

6.2.0

What version of Node.js are you using?

22.13.0

What operating system are you using?

Windows 10

What CPU architecture are you using?

x86_64 intel 13600KF

What Node versions, OSs and CPU architectures are you building for?

node22-win-x64

Describe the Bug

Warning Cannot resolve 'path.join(dir_path, file)'
  E:\Projects\web\zjq-admin\models\index.js
  Dynamic require may fail at run time, because the requested file
  is unknown at compilation time and not included into executable.
  Use a string literal as an argument for 'require', or leave it
  as is and specify the resolved file name in 'scripts' option.

Expected Behavior

How to solve it ?

To Reproduce

package.json

 "scripts": [
  "./bin/www.js",
  "./models/examBatch.js",
  "./models/grade.js",
  "./models/user.js",
  "./models/worker.js",
  "./models/workerExamBatch.js"
  ],

Warning file in ./model/index.js

fs.readdirSync(__dirname)
  .filter((file) => {
    return (
      file.indexOf(".") !== 0 &&
      file !== basename &&
      file.slice(-3) === ".js" &&
      file.indexOf(".test.js") === -1
    );
  })
  .forEach((file) => {
    const model = require(path.join(__dirname, file))(
      sequelize,
      Sequelize.DataTypes,
    );
    models[model.name] = model;
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions