I need little help with building custome npm command to run ES module file, I am using this file to seed my database.
i am using sequelize in a Node api (not using sequelize CLI or migrations) i want to make an npm command to run the seeding file (it is just a custom writen .js file with import statements to seed the modals) i have type=module in my package.json. means i am using ES module in all my project, I tried adding the command 'db-seed:'node /commands/db-seed.js', but now whenever I run this command it gives me an error showing that the node instead of using the current project directory which is "f:/laragon/www/kanban-board-app/", uses base directory F:/, although I am running this command from my project's directory due to this it is not able to find my import statement modules that are available in project's root directory.
what I tried to solve this problem: if I use process.cwd() it gives an error unexpected identifier process, even I have imported process from node:process
PS F:\laragon\www\kanban-board-node-app> node commands/db-seed.js
file:///F:/laragon/www/kanban-board-node-app/commands/db-seed.js:18
import { hashMake } from process.cwd("../utils/helpers.js");
^^^^^^^
SyntaxError: Unexpected identifier 'process'
at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:166:18)
at callTranslator (node:internal/modules/esm/loader:416:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:422:30)
at async link (node:internal/modules/esm/module_job:88:21)
Node.js v22.0.0
PS F:\laragon\www\kanban-board-node-app>
if I do relative path solution which is
import path from 'path';
import { fileURLToPath } from 'url';
// Resolve the current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import {hashMake} from path.resolve(__dirname, '../utils/helpers.js');
it gives me an error unexpected identifier path(pointing to line path.resolve);
it doesn't leave any other options for me to try. And I still unable to run that js file using npm commands
here is my package.json
{
"name": "kanban-board-app",
"description": "This is a kanban board app based on node js express and mongodb",
"private": true,
"version": "0.0.0",
"type": "module",
"main":"server.js",
"scripts": {
"dev": "nodemon –experimental-modules server.js",
"db-refresh":"node commands/db-refresh.js",
"db-seed":"node commands/db-seed.js",
"watch": "node --watch server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Zeeshan",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"cloudinary": "^2.0.1",
"concurrently": "^8.2.2",
"config": "^3.3.11",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"datauri": "^4.1.0",
"dayjs": "^1.11.10",
"dotenv": "^16.4.2",
"express": "^4.18.2",
"express-async-errors": "^3.1.1",
"express-mongo-sanitize": "^2.2.0",
"express-rate-limit": "^7.1.5",
"express-validator": "^7.0.1",
"helmet": "^7.1.0",
"http-status-codes": "^2.3.0",
"immer": "^10.1.1",
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"mysql2": "^3.9.3",
"nanoid": "^5.0.5",
"nodemailer": "^6.9.14",
"nodemon": "^3.0.3",
"react-select": "^5.8.0",
"sequelize": "^6.37.2",
"winston": "^3.11.0"
},
"devDependencies": {
"@eslint/js": "^9.1.1",
"@types/multer": "^1.4.11",
"@types/nodemailer": "^6.4.15",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
"globals": "^15.1.0",
"ts-node": "^10.9.2"
}
}
and here are my import statements from "./commands/db-seed.js" file code
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import process,{cwd} from "node:process";
import sequelize from "../db.js";
import { v1 as uuidv1, v4 as uuidv4 } from "uuid";
// Resolve the path to helpers.js
// const helpersPath = path.resolve(__dirname, '../utils/helpers.js');
//
// import { hashMake } from helpersPath;
import { hashMake } from process.cwd("../utils/helpers.js");
import User from "../models/UserModel.js";
import Workspace from "../models/Workspace.js";
import Board from "../models/BoardModel.js";
import UserWorkspace from "../models/UserWorkspace.js";
import BoardColumn from "../models/BoardColumnModel.js";
import Task from "../models/TaskModel.js";
import SubTask from "../models/SubTask.js";
import TaskDiscussion from "../models/TaskDiscussion.js";
import {
trunOffForeignKeyCheckAndTruncateTable,
checkIfTableExists,
} from "../../utils/helpers.js";
You will not be able to use the import x from './y'
syntax to accomplish this, as it's not possible to make the ./y
part dynamic.
However, you can use the import()
function.
Example:
const myModule = await import(`some/path/that/can/even/include/a${variable}`);