首页 > 解决方案 > Run several javascript file dynamically using same npm run command

问题描述

My folder structure:

app

  Model

    user.js
    post.js

My package.json file

"scripts": {
    "migrate": "node ./app/Model/"
}

I want to run javascript file in command line dynamically.

Like:

npm run migrate user.js
npm run migrate post.js

Is there any way to achieve this?

标签: javascriptnode.jsexpressnpm

解决方案


You could write a script that dynamically requires the decired js file.

"scripts": {
    "migrate": "node model.js"
}

Then model.js like this:

const path = require('path');    
require(path.join(__dirname, 'app', 'Model', process.argv[2]));

推荐阅读