node.js - Nodemon 在尝试使用 es6 模块时崩溃,但在 es5 上运行良好
问题描述
我正在尝试使用 es6 模块运行服务器,但每次执行时都会崩溃,并且每当我使用 es5错误消息时都可以正常工作
我已经安装了 babel 并且在我的 .babelrc 文件中有 "preset": ["env"] 但是每当我运行它时,我都会遇到 "语法错误:无效或意外的令牌"。这不是一个特定的项目,这是我正在经历的第三个项目
import http from 'http';
import express from 'express';
import logger from 'morgan';
import bodyParser from 'body-parser';
// setting up express application
const app = express();
const hostName = '127.0.0.1';
const port = 3000;
const server = http.createServer(app);
// logs request to the console
app.use(logger('dev'))
// Parse incoming data requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// making a request to the server
app.get('*', (req, res) => res.status(200).send({
message: 'Welcome to the default API route',
}));
server.listen(port, hostName, () => {
console.log(`Server running at http://${hostName}:${port}/`);
});
它应该向控制台显示“欢迎使用默认 API 路由”,但它却是一条错误消息。如果需要回购,我很乐意提供
解决方案
默认情况下,Node 运行时还不支持 ES6。您可以像这样集成它:
npm i esm && npm i -D nodemon
在您的 package.json 中,将其添加到脚本中:
"start": "nodemon -r esm index.js"
(确保index.js
脚本的一部分与您的服务器入口点文件的名称匹配)
- 跑
npm start
推荐阅读
- javascript - 异步数组缓冲区中的“未定义 Blob”错误。如何解决?
- python - 如何解决从错误的 json 格式解码的问题
- google-cloud-dataflow - Apache Beam/Dataflow:每次部署初始化状态的最佳方式
- css - Symfony/Twig - 样式化表单小部件
- javascript - Javascript 函数中的嵌套 If 逻辑和位运算符
- uitableview - 如何在两行之间添加n个自定义单元格
- shell - shell排序命令-k POS1,POS2 无效
- shell - 使用 sed 更新特定字段
- node.js - 附加 ObjectIds 数组
- python-3.x - Edx 课程/Python 基础/使用 pop()、while、insert 反转字符串