简体   繁体   English

如何使用Node JS设置Babel 6以在服务器端代码中使用ES6?

[英]How do I setup Babel 6 with Node JS to use ES6 in my Server Side code?

I have read several times the documentation provided at : Node API Babel 6 Docs 我已经阅读了几次提供的文档: Node API Babel 6 Docs

I'm starting out learning pg-promise following the Learn by Example tutorial and would prefer to work with ES6 and transpile to ES5 with Babel but am unsure of a few things : 我将在“通过示例学习”教程之后开始学习pg-promise,并希望使用ES6并通过Babel转换为ES5,但不确定以下几点:

  1. After installing babel-core, what preset do I use and where/how do I configure this to work? 安装babel-core后,我要使用什么预设?如何在哪里/如何配置它以起作用?

The documentation was unclear to me about which file I put: require("babel-core").transform("code", options); 对于我放入哪个文件,文档尚不清楚: require(“ babel-core”)。transform(“ code”,options); into and what parts of that code are place holders. 占位符以及该代码的哪些部分。 When I use that code, do I just use it one time somewhere and then I can use ES6 in every other file? 使用该代码时,是否只在某个地方使用过一次,然后可以在其他所有文件中使用ES6? How would this be achieved? 如何实现?

  1. I read about this .babelrc file and would like to confirm if the actual filename is ".babelrc" or if that is just the file extension and where in relation to the root directory of my project do I put that file.. and how do I link to it? 我阅读了有关此.babelrc文件的信息,并想确认实际的文件名是否为“ .babelrc”或仅仅是文件扩展名以及相对于我项目根目录的位置,我将该文件放置在该位置。我链接到它吗?

  2. If I'm using pg-promise should I be using ES6 and Babel or will running : npm install as described under the Testing section for pg-promise be enough and trying to use ES6 with this create more problems? 如果我使用的是pg-promise ,我应该使用ES6和Babel还是要运行:如pg-promise的“ 测试”部分所述运行npm install就足够了,并尝试在此方面使用ES6会产生更多问题吗?

I was hoping to take advantage of let and const if the need came up during my server side development. 如果我在服务器端开发期间遇到了需求,我希望利用let和const的优势。

  1. Is there a standard file structure for a node+babel+pg-promise server setup? 节点+ Babel + pg-Promise服务器设置是否有标准文件结构?

Edit Worth noting that I have also read Node JS with Babel-Node and saw that using this should be avoided. 编辑值得指出的是,我也看到了节点JS用巴贝尔节点 ,看到使用这个应该避免。 The final answer at the very bottom didn't really make sense to me for similar reasons I'm having trouble following the actual documentation provided by Babel. 由于类似的原因,我在遵循Babel提供的实际文档时遇到了麻烦,但最底层的最终答案对我来说并没有什么意义。

1.a What Preset is needed? 1.a需要什么预设?

You will need to install Babel firstly with npm install babel-core --save-dev in the root directory of your project using a Terminal window like Command Prompt. 您首先需要使用“命令提示符”之类的“终端”窗口在项目的根目录中使用npm install babel-core --save-dev安装Babel。

Once installed, you will need to install the es2015 preset with npm install babel-preset-es2015 --save-dev . 安装完成后,您需要使用npm install babel-preset-es2015 --save-dev安装es2015预设 Babel-Core is Promises/A+ Compliant but not ideal for usage due to poor error handling so a library such as Bluebird should be used instead for this purpose. Babel-Core符合Promises / A +, 由于差错处理不佳而不适用于此用途,因此应为此目的使用诸如Bluebird之类的库。 In order to transpile, babel-core will still need to be installed and es2015 enables ES6->ES5 transpiling so you can use fancy things like let and const etc. 为了进行转译,仍然需要安装babel-core,并且es2015启用ES6-> ES5转译,以便您可以使用let和const等奇特的东西。

1.b Where to put require("babel-core"); 1.b在哪里放置require("babel-core"); ?

instead, use require("babel-core/register"); 而是使用require("babel-core/register"); and place it inside your Entry file typically called, "server.js" . 并将其放在通常称为“ server.js”的 Entry文件中。 The server.js file will need to use CommonJS (ES5) exclusively. server.js文件将需要专门使用CommonJS(ES5)。

By using the "require" statement it will apply all relevant transforms to all code being required into the Entry file and all files being required/included into those files. 通过使用“ require”语句,它将把所有相关的转换应用到Entry文件中所有需要的代码,以及这些文件中需要/包含的所有文件。

You point to the Entry file inside package.json under the "main": section. 您指向package.json中"main":部分下的Entry文件。

Package.json is created when you initialise the project with npm init at the root directory of your project inside the Terminal Window 当在终端窗口中项目的根目录中使用npm init初始化项目时,将创建Package.json。

One approach to this would be : 一种解决方法是:

  • Entry File - server.js 条目文件 -server.js
  • server.js - requires {babel-core and the main ES6 file : config.js/jsx/es6/es} server.js-需要{babel-core和主要ES6文件:config.js / jsx / es6 / es}
  • config.es6 - uses ES6 and has includes(requires) for all other project files that can also use ES6 as they get transpiled by being loaded into the "config" file which is being directly transpiled by babel-core. config.es6-使用ES6,并且包含所有其他项目文件的includes(要求),这些项目文件也可以使用ES6,因为它们通过加载到由babel-core直接编译的“ config”文件中而被编译。

2. What is .babelrc? 2.什么是.babelrc?

.babelrc is the filename and should be placed in the same folder as your package.json file (normally the root directory) and will automatically "load" when babel-core is required to determine which preset(s) or plugins are to be used. .babelrc是文件名,应与package.json文件位于同一文件夹(通常是根目录),并且在需要babel-core以确定要使用哪些预设或插件时会自动“加载” 。

Inside .babelrc , you will need to add the following code : .babelrc ,您需要添加以下代码:

{
  "presets": ["es2015"]
}

3. pg-promise Testing Section 3. pg-promise测试科

A direct quote from the developer recently answered this 开发者的直接报价最近回答了这个问题

You do not need to worry about steps in the Tests, use only the steps in the install. 您无需担心测试中的步骤,只需使用安装中的步骤即可。 The one in tests relates to the dev dependency installation, in order to run tests. 测试中的一个与开发依赖安装有关,以便运行测试。 The pg-promise can work with any promise library compliant with Promises/A+ spec. pg-promise可以与任何符合Promises / A +规范的Promise库一起使用。

4. Standard File/Folder Structure for Server Side Projects? 4.服务器端项目的标准文件/文件夹结构?

There is no standard way to achieve this task as each project has unique demands. 由于每个项目都有独特的需求,因此没有标准的方法来完成此任务。 A good starting point would be to place the Entry file in the project root directory, the ES6 Config file in a "scripts" or "src" sub-folder and individual components in folders below that. 一个很好的起点是将Entry文件放置在项目根目录中,将ES6 Config文件放置在“脚本”或“ src”子文件夹中,并将各个组件放置在其下方的文件夹中。

eg 例如

  • ROOT/server.js 根目录/server.js
  • ROOT/src/config.es6 根目录/src/config.es6
  • ROOT/src/component1/files.es6 根目录/src/component1/files.es6
  • ROOT/src/component2/files.es6 根目录/src/component2/files.es6

With this in place, Babel will successfully transpile all ES6 to ES5 and enable support of A+ compliant promises. 通过这样做,Babel将成功地将所有ES6移植到ES5,并支持A +兼容承诺。

To begin using the node.js webserver This Guide provides a bit more insight and in the context of this answer the code shown would be placed into the ES6 config.es6 file and the following code would go into the Entry server.js file : 开始使用node.js网络服务器本指南提供了更多的见解,在此答案的上下文中,显示的代码将放入ES6 config.es6文件,而以下代码将进入Entry server.js文件:

require("babel-core/register");
require("./src/config.es6");

The process for building Isomorphic web applications is different to this and would likely use things like grunt, gulp, webpack, babel-loader etc another example of which can be Found Here . 构建同构Web应用程序的过程与此不同,可能会使用grunt,gulp,webpack,babel-loader等,其中另一个示例可以在此处找到

This answer is the combination of several key points provided by other answers to this question as well as contributions from experienced developers and my own personal research and testing. 该答案是该问题的其他答案提供的几个关键点的组合,以及经验丰富的开发人员和我自己的个人研究与测试的贡献。 Thank you to all who assisted in the production of this answer. 感谢所有协助制作此答案的人。

  1. This answer uses this simple directory structure 此答案使用此简单的目录结构

    project/server/src/index.js => your server file project/server/src/index.js =>您的服务器文件

    project/server/dist/ => where babel will put your transpiled file project/server/dist/ => babel会将您转译的文件放在其中

  2. Install babel dependencies 安装babel依赖项

    npm install -g babel nodemon

    npm install --save-dev babel-core babel-preset-es2015

  3. Add these npm scripts to your package.json file 将这些npm脚本添加到package.json文件中

    "scripts": { "compile": "babel server/src --out-dir server/dist", "server": "nodemon server/dist/index.js }

  4. Create a .babelrc file in your project root directory 在项目根目录中创建一个.babelrc文件

    { "presets": "es2015" }

  5. Transpile your directory with 用以下命令转换目录

    npm run compile

  6. Run your server with 使用以下命令运行服务器

    npm run server

I think you should use a tool like grunt or gulp to manage all your "build" tasks. 我认为您应该使用grunt或gulp之类的工具来管理所有“构建”任务。 It will automate it for you, and you won't make errors. 它会自动为您完成操作,并且不会出错。

In one command, you can transpile your code into babel ES2015 et start your application. 在一个命令中,您可以将代码转换为babel ES2015并启动应用程序。

I suggest you to take a look at this simple project . 我建议您看一下这个简单的项目 (just install node_modules and launch npm start to start the app.js file) (只需安装node_modules并启动npm start即可启动app.js文件)

However, if you really want to use babel manually, 但是,如果您真的想手动使用babel,

  • .babelrc is the name of the file, you can see one in this project (redux) to have an example .babelrc是文件名,您可以在该项目(redux)中看到一个示例

  • .babelrc is a config file, if you want to see how it works, you can check this package.json (always redux) .babelrc是一个配置文件,如果您想查看它的工作方式,可以检查此package.json(始终为redux)

  • There's actually no standard way that I know. 我实际上没有标准的方法。 You can use the project skeleton below if needed, and send pull request to improve it :-) 您可以根据需要使用下面的项目框架,并发送拉取请求以进行改进:-)

@makeitsimple @makeitsimple

Step: 1 步骤1

npm install nodemon --save

In project directory 在项目目录中

Step: 2 第2步

yarn add babel-cli

yarn add babel-preset-es2015

Step: 2 In package.json-> scipts change 'start' to the following 步骤:2在package.json-> scipts中将“ start”更改为以下内容

start: "nodemon src/server.js --exec babel-node --presets es2015"

Step: 3 步骤:3

yarn start

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

相关问题 我如何使用 express-generator 设置 babel 以使用 es6 - How i setup babel with express-generator to use es6 如何使用babel-core / register在node.js中编写ES6代码? - How to use babel-core/register for writing ES6 code in node.js? 我可以在 ES6 中使用服务器端节点 js 并且仍然不使用 bable - Can I use server-side node js with ES6 and still without using bable 我应该使用ES6来转换我的节点js代码吗? - Should I transpile my node js code for using ES6? 在没有 Babel 的情况下编写服务器端 JavaScript 时如何使用 ES6 特性? - How to use ES6 features when writing server-side JavaScript without Babel? 我可以在没有Babel的Node.js中使用ES6 Javascript吗? - Can I use ES6 Javascript in Node.js without Babel? 编写 ES6 代码时如何使用 Babel 部署 Node.js / React 应用程序到 Heroku? - How to deploy Node.js / React application to Heroku using Babel when writing ES6 code? 如何在 node.js 中使用 es6 模块 - How can i use es6 modules in node.js Babel成功地转译了ES6代码(node.js),但是当“ npm start”执行时,它会抛出“ SyntaxError:意外的令牌导入” - Babel transpiled ES6 code (node.js) successfully, but when do “npm start” it throws “SyntaxError: Unexpected token import ” 如何设置babel-cli在命令行上运行本机ES6 JavaScript代码? - How to setup babel-cli to run react-native ES6 JavaScript code on command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM