鍍金池/ 問答/HTML/ Nodejs里運行npm run dev shell腳本無效果?

Nodejs里運行npm run dev shell腳本無效果?

問題描述

Nodejs里如何運行npm run dev shell腳本?

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

1、在webpack構(gòu)建的模塊里,npm run script一個node腳本文件,但是我想在這個腳本文件里運行其它的npm run,嘗試了require('child_process').exec但是無效,執(zhí)行到這里什么反應(yīng)都沒。

相關(guān)代碼

var exec = require('child_process').execSync
exec('npm run dev')

你期待的結(jié)果是什么?實際看到的錯誤信息又是什么?

或者在node腳本js文件里如何才能正確執(zhí)行package.json里的script呢?

補充貼上package.json內(nèi)容

{
  "name": "vue",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "author": "author@gmail.com",
  "private": true,
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "modify": "node script/modify-path.js",
    "build:prod": "npm run modify && npm run build --no-cache",
    "start": "npm run dev",
    "lint": "eslint --ext .js,.vue src",
    "build": "node build/build.js"
  },
  "dependencies": {
    "fg-loadcss": "^2.0.1",
    "vue-router": "^3.0.1"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^8.2.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "commander": "^2.16.0",
    "copy-webpack-plugin": "^4.0.1",
    "cross-env": "^5.2.0",
    "css-loader": "^0.28.0",
    "eslint": "^4.19.1",
    "eslint-config-standard": "^11.0.0",
    "eslint-friendly-formatter": "^4.0.1",
    "eslint-loader": "^2.0.0",
    "eslint-plugin-import": "^2.13.0",
    "eslint-plugin-node": "^6.0.1",
    "eslint-plugin-promise": "^3.8.0",
    "eslint-plugin-standard": "^3.1.0",
    "eslint-plugin-vue": "^4.7.0",
    "execa": "^0.10.0",
    "file-loader": "^1.1.11",
    "fontfaceobserver": "^2.0.13",
    "fontmin": "^0.9.7-beta",
    "fontmin-webpack": "^2.0.1",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "html-webpack-plugin": "^3.2.0",
    "inquirer": "^6.0.0",
    "js-yaml": "^3.12.0",
    "mini-css-extract-plugin": "^0.4.1",
    "node-notifier": "^5.1.2",
    "node-sass": "^4.9.2",
    "optimize-css-assets-webpack-plugin": "^5.0.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "postcss-url": "^7.2.1",
    "rimraf": "^2.6.0",
    "sass-loader": "^7.0.3",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^1.0.1",
    "vue": "^2.5.16",
    "vue-loader": "^15.2.4",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webfont-webpack-plugin": "^0.2.2",
    "webpack": "^4.16.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 6.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}
回答
編輯回答
別瞎鬧

不是太明白你想要實現(xiàn)的效果,或者,你想要做什么
https://docs.npmjs.com/misc/s...
你想要的,是否是這樣呢

{
  "name": "test",
  "main": "server.js",
  "scripts": {
    "start": "npm run build && npm run dev ",
    "dev": "", // 做點什么
    "build": "" // 做點什么
  },
  "dependencies": {
  }
}
2018年7月14日 09:51
編輯回答
別硬撐

這就是你想要的

var spawn = require('child_process').spawn;

spawn('npm', ['run','dev'], {
    stdio: 'inherit'
});

var exec = require('child_process').execSync;
exec('npm run dev', {stdio: 'inherit'});
2017年8月19日 01:45