2
2
3
3
### Getting Started
4
4
5
- First we'll install ` babel- cli ` and ` babel- preset-env ` .
5
+ First we'll install ` @ babel/ cli` , ` @babel/core ` and ` @ babel/ preset-env` .
6
6
7
7
``` shell
8
- $ npm install --save-dev babel- cli babel- preset-env
8
+ $ npm install --save-dev @ babel/ cli @ babel/core @babel/ preset-env
9
9
```
10
10
11
11
Then we'll create a ` .babelrc ` file for configuring babel.
@@ -18,7 +18,7 @@ This will host any options we might want to configure `babel` with.
18
18
19
19
``` json
20
20
{
21
- "presets" : [" env" ]
21
+ "presets" : [" @babel/preset- env" ]
22
22
}
23
23
```
24
24
@@ -30,12 +30,14 @@ $ touch index.js
30
30
``` js
31
31
import http from ' http' ;
32
32
33
- http .createServer ((req , res ) => {
33
+ const server = http .createServer ((req , res ) => {
34
34
res .writeHead (200 , {' Content-Type' : ' text/plain' });
35
35
res .end (' Hello World\n ' );
36
36
}).listen (1337 , ' 127.0.0.1' );
37
37
38
38
console .log (' Server running at http://127.0.0.1:1337/' );
39
+
40
+ export default server ;
39
41
```
40
42
41
43
With recent changes to babel, you will need to transpile your ES6 before node can run it.
@@ -171,22 +173,23 @@ $ touch test/index.js
171
173
import http from ' http' ;
172
174
import assert from ' assert' ;
173
175
174
- import ' ../lib/index.js' ;
176
+ import server from ' ../lib/index.js' ;
175
177
176
178
describe (' Example Node Server' , () => {
177
179
it (' should return 200' , done => {
178
180
http .get (' http://127.0.0.1:1337' , res => {
179
181
assert .equal (200 , res .statusCode );
182
+ server .close ();
180
183
done ();
181
184
});
182
185
});
183
186
});
184
187
```
185
188
186
- Next, install ` babel- register ` for the require hook.
189
+ Next, install ` @ babel/ register` for the require hook.
187
190
188
191
``` shell
189
- $ npm install --save-dev babel- register
192
+ $ npm install --save-dev @ babel/ register
190
193
```
191
194
192
195
Then we can add an ` npm test ` script.
@@ -196,7 +199,7 @@ Then we can add an `npm test` script.
196
199
"start": "nodemon lib/index.js --exec babel-node",
197
200
"build": "babel lib -d dist",
198
201
"serve": "node dist/index.js",
199
- + "test": "mocha --require babel- register"
202
+ + "test": "mocha --require @ babel/ register"
200
203
}
201
204
```
202
205
0 commit comments