简体   繁体   English

如何在 npm 脚本中使用 nodemon 来构建和启动脚本?

[英]How to use nodemon in npm scripts to build and start scripts?

"scripts": {
  "build": "babel src -d lib",
  "start": "node --use_strict ./lib/index.js",
  "watch": "nodemon lib/index.js --exec npm run build"
}

Using the command npm run watch results in the following wrong command being run: [nodemon] starting "npm lib/index.js run build"使用命令npm run watch导致npm run watch以下错误命令: [nodemon] starting "npm lib/index.js run build"

How would I write a nodemon command that, on reload, transpiles the code using babel and reloads the code?我将如何编写一个 nodemon 命令,在重新加载时,使用 babel 转换代码并重新加载代码?

You could simply run your code with babel-node to avoid explicit transpiling.您可以简单地使用babel-node运行您的代码,以避免显式转译。

$ nodemon lib/index.js --exec babel-node --presets=es2015,stage-2

It seems like this is the recommended way to use nodemon with babel .似乎这是将nodemonbabel一起使用的推荐方法。

Please note, running --exec could have unintended side effects when running your development environment remotely of your localhost请注意,在远程运行localhost development环境时,运行--exec可能会产生意想不到的副作用

You can have two nodemons, one to transpile and the other to run your code.您可以有两个 nodemon,一个用于转译,另一个用于运行您的代码。 In package.json you can do something like this:在 package.json 你可以做这样的事情:

"scripts": {
    "serve": "nodemon --watch dist/ ./dist/index.js",
    "build" : "nodemon --watch src/ --exec babel ./src --out-dir ./dist --source-maps --copy-files"
  },

A better option would be to not use a global install but instead use the package installed locally.更好的选择是不使用全局安装,而是使用本地安装的包。 This will also help for automation builds that might be setup the same as your local machine per 12 factor app design.这也有助于自动化构建,这些构建可能与每个 12 因子应用程序设计的本地机器设置相同。

"scripts": {
"watch": "node ./node_modules/nodemon/bin/nodemon.js"

} }

"scripts": {
  "build": "babel src -d lib",
  "start": "nodemon --exec babel-node lib/index.js",
  "serve": "npm run build && node lib/index.js"
}

Serve is for production, with npm start what you do is transpile first and then run nodemon. Serve 用于生产,使用 npm start 你要做的是先转译,然后运行 ​​nodemon。

There is an option to build files with Babel in "watch" mode, let Nodemon monitor just the "build" folder and restart the app upon changes to the compiled output.有一个选项可以在“监视”模式下使用 Babel 构建文件,让 Nodemon 仅监视“构建”文件夹,并在编译输出发生更改时重新启动应用程序。

{
  "name": "app",
  "version": "1.0.0",
  "private": true,
  "dependencies": {},
  "devDependencies": {
    "@babel/cli": "^7.6.0",
    "@babel/core": "^7.6.0",
    "@babel/preset-env": "^7.6.0",
    "nodemon": "^1.19.2"
  },
  "scripts": {
    "build": "babel src --out-dir build --source-maps=inline --verbose",
    "start": "yarn build --watch & sleep 1 && nodemon --watch build build/index.js"
  }
}

在此处输入图片说明

This example is taken from GraphQL API Examples repository on GitHub.此示例取自 GitHub 上的GraphQL API Examples存储库。

  "scripts": {
    "build": "babel src -d lib",
    "start": "node --use_strict ./lib/index.js",
    "watch": "nodemon --exec \"npm run build && node lib/index.js\" -e js --ignore lib/"
  }

Then run npm run watch .然后运行npm run watch After this, nodemon will rebuild the project and then restart the server every time source code( .js files) is modified.此后,每次修改源代码( .js文件)时,nodemon 都会重建项目,然后重新启动服务器。

--exec specifies what non-node scripts (also works for node scripts as above node lib/index.js ) you want nodemon to execute when there is a file change. --exec指定您希望 nodemon 在文件更改时执行哪些非节点脚本(也适用于节点node lib/index.js上面的节点脚本)。

-e specifies what file extensions you want nodemon to watch. -e指定您希望 nodemon 监视的文件扩展名。

--ignore specifies the files/directories you want nodemon to ignore. --ignore指定您希望 nodemon 忽略的文件/目录。 This option is essential to solve this problem because if you do not specify to ignore this lib/ folder, nodemon will restart infinitely as the compiled files in lib/ are also .js files.此选项对于解决此问题至关重要,因为如果您不指定忽略此lib/文件夹,则 nodemon 将无限重启,因为lib/中的编译文件也是.js文件。

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

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