简体   繁体   English

如何运行写在外部js文件中的npm脚本(package.json)?

[英]How to run npm script (package.json) written in an external js file?

I know it has been covered in different questions but mine is a bit different: Sorry in advance if it sounds really noob.我知道它已经涵盖在不同的问题中,但我的有点不同:如果听起来真的很菜,请提前抱歉。

this is the script in package.json:这是 package.json 中的脚本:

"start": "nodemon ./index.js --exec \"node -r babel-register\"",

I replaced by :我替换为:

"start": "node scripts/start.js",

and in start.js, i do :在 start.js 中,我这样做:

const { execSync } = require('child_process')

execSync('nodemon ../index.js --exec \"node -r babel-register\"')

wich throw an error :引发错误:

/bin/sh: nodemon: command not found

Am I right with "execSync" ?我对“execSync”的看法正确吗?

I tried import nodemon in the file but it obviously not helping我尝试在文件中导入 nodemon 但它显然没有帮助

What you're doing should work if nodemon is installed globally, ie with:如果nodemon是全局安装的,那么你正在做的事情应该可以工作,即:

npm install -g nodemon

But if it's installed as a project dependency, ie with:但如果它作为项目依赖项安装,即:

npm install --save-dev nodemon

Then you'll need to run it from the directory containing all the locally installed binaries: node_modules/.bin/然后你需要从包含所有本地安装的二进制文件的目录中运行它: node_modules/.bin/

So something like this should work:所以这样的事情应该有效:

execSync('./node_modules/.bin/nodemon ../index.js --exec \"node -r babel-register\"')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM