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
Assuming this is a local file in your project and not another module under
node_modules
, you need to prefix the path with./
:You’re missing
./
in yourrequire
.Third-party modules that you bring in via npm (e.g.
npm install sequelize
) can be specified (torequire
) 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:The use of
./
is how it disambiguates using third-party source files that npm installed into yournode_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 calleddebug.js
in your current working directory.)