我已经检查了几个答案和指南,但无法使所有部分协同工作。
定义了一个非常简单的Node / Express项目(test2) 主文件是index.js
这是我的project.js
{
"name": "test2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"babel": {
"presets": [ "es2017" ]
},
"scripts": {
"babel-node": "babel-node --presets=es2017",
"start": "nodemon --exec npm run babel-node -- ./dist/index.js",
"build": "babel src -d dist"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-preset-env": "^1.6.1",
"express": "^4.16.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babel-register": "^6.11.6",
"nodemon": "^1.10.0",
"ws": "3.3.2"
}
}
我的一个文件中有一个es6命令:console.log(Date.now()。toDateString()+“:”+ text.padStart(indent));
我执行了npm start并构建了dist库, 但是它看起来在这个库下它没有改变代码,我也可以看到padStart和toDateString。 (在上面使用'严格' - 所以确实发生了一些事情)
当我尝试运行它时,我得到了:
> npm start
> test2@1.0.0 start D:\MyData\newJSTests\express\test2
> nodemon --exec npm run babel-node -- ./dist/index.js
[nodemon] 1.14.11
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm run babel-node ./dist/index.js`
> test2@1.0.0 babel-node D:\MyData\newJSTests\express\test2
> babel-node --presets=es2017 "./dist/index.js"
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
...
我尝试过放入project.js:
"scripts": {
"start": "nodemon dist/index.js --exec babel-node --presets es2017,stage-2",
"build": "babel lib -d dist",
"serve": "node dist/index.js",
"test": "mocha --compilers js:babel-register"
},
但是得到同样的问题。 我可能错过了一些东西。会感激一些帮助。 感谢
编辑: 我的错误 - 关于端口,但是, 看起来巴贝尔没有很好的转换。
D:\MyData\newJSTests\express\test2>npm start
> test2@1.0.0 start D:\MyData\newJSTests\express\test2
> nodemon --exec npm run babel-node -- ./dist/index.js
[nodemon] 1.14.11
[nodemon] ...
[nodemon] starting `npm run babel-node ./dist/index.js`
> test2@1.0.0 babel-node D:\MyData\newJSTests\express\test2
> babel-node --presets=es2017 "./dist/index.js"
listening on port 3000!
TypeError: Date.now(...).toDateString is not a function
at Object.myLogger (D.../dist/tools/logger.js:8:36)