Nodejs里运行npm run dev shell脚本无效果?

问题描述

Nodejs里如何运行npm run dev shell脚本?

问题出现的环境背景及自己尝试过哪些方法

1、在webpack构建的模块里,npm run script一个node脚本文件,但是我想在这个脚本文件里运行其它的npm run,尝试了require('child_process').exec但是无效,执行到这里什么反应都没。

相关代码

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

你期待的结果是什么?实际看到的错误信息又是什么?

或者在node脚本js文件里如何才能正确执行package.json里的script呢?

补充贴上package.json内容

{
  "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"
  ]
}
阅读 10.2k
2 个回答

这就是你想要的

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

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

var exec = require('child_process').execSync;
exec('npm run dev', {stdio: 'inherit'});

不是太明白你想要实现的效果,或者,你想要做什么
https://docs.npmjs.com/misc/s...
你想要的,是否是这样呢

{
  "name": "test",
  "main": "server.js",
  "scripts": {
    "start": "npm run build && npm run dev ",
    "dev": "", // 做点什么
    "build": "" // 做点什么
  },
  "dependencies": {
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏