skip to Main Content

So im trying to add a currency to my bot and when i do node dbinit.js it gives me module not found.
this is my script and thats the error that it gives me.

const Sequelize = require('sequelize');

const sequelize = new Sequelize('database', 'username', 'password', {
    host: 'localhost',
    dialect: 'sqlite',
    logging: false,
    storage: 'database.sqlite',
});

const CurrencyShop = require('models/currencyshop.js')(sequelize, DataTypes);
require('Mr.House/models/Users.js')(sequelize, DataTypes);
require('Mr.House/models/UserItems.js')(sequelize, DataTypes);

const force = process.argv.includes('--force') || process.argv.includes('-f');

sequelize.sync({ force }).then(async () => {
    const shop = [
        CurrencyShop.upsert({ name: 'Tea', cost: 1 }),
        CurrencyShop.upsert({ name: 'Coffee', cost: 2 }),
        CurrencyShop.upsert({ name: 'Cake', cost: 5 }),
    ];

    await Promise.all(shop);
    console.log('Database synced');

    sequelize.close();
}).catch(console.error);

Error: Cannot find module ‘models/currencyshop.js’

i triedadding the main folder name but nothing seems to work.

2

Answers


  1. Assuming this is a local file in your project and not another module under node_modules, you need to prefix the path with ./:

    const CurrencyShop = require('./models/currencyshop.js')(sequelize,DataTypes);
    // Here ----------------------^
    
    Login or Signup to reply.
  2. You’re missing ./ in your require.

    Third-party modules that you bring in via npm (e.g. npm install sequelize) can be specified (to require) using just the name of the module. e.g. require('sequelize').

    But your own JavaScript that files that exist in your source directory need to be specified with a relative path, prefixed with ./.
    e.g.: require('./models/currencyshop.js')

    Read the docs

    The documentation for require says:

    Modules can be imported from node_modules. Local modules and JSON files can be imported using a relative path (e.g. ./, ./foo, ./bar/baz, ../foo) that will be resolved against the directory named by __dirname (if defined) or the current working directory.

    The use of ./ is how it disambiguates using third-party source files that npm installed into your node_modules folder (e.g.: require('debug') debug is a famous npm package available on npm) vs using a file in your source directory (e.g.: require('./debug') could be used to specify a file called debug.js in your current working directory.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search