Javascript Can';无法启动节点应用程序(server.js,可能是Babel问题?)
我正在尝试让我的节点应用程序以npm start启动。应用程序在启动时崩溃,我不知道原因。我对Node和所有东西都很陌生。有人能理解发生了什么事吗 当我运行npm start--verbose时,我得到以下结果:Javascript Can';无法启动节点应用程序(server.js,可能是Babel问题?),javascript,node.js,npm,babeljs,Javascript,Node.js,Npm,Babeljs,我正在尝试让我的节点应用程序以npm start启动。应用程序在启动时崩溃,我不知道原因。我对Node和所有东西都很陌生。有人能理解发生了什么事吗 当我运行npm start--verbose时,我得到以下结果: hunterhawley@hunter-test:~$ npm start --verbose npm info it worked if it ends with ok npm verb cli [ '/home/hunterhawley/.nvm/versions/node/v11
hunterhawley@hunter-test:~$ npm start --verbose
npm info it worked if it ends with ok
npm verb cli [ '/home/hunterhawley/.nvm/versions/node/v11.10.1/bin/node',
npm verb cli '/home/hunterhawley/.nvm/versions/node/v11.10.1/bin/npm',
npm verb cli 'start',
npm verb cli '--verbose' ]
npm info using npm@6.7.0
npm info using node@v11.10.1
npm verb config Skipping project config: /home/hunterhawley/.npmrc. (matches userconfig)
npm verb run-script [ 'prestart', 'start', 'poststart' ]
npm info lifecycle mern-starter@2.0.0~prestart: mern-starter@2.0.0
npm info lifecycle mern-starter@2.0.0~start: mern-starter@2.0.0
> mern-starter@2.0.0 start /home/hunterhawley
> cross-env BABEL_DISABLE_CACHE=1 NODE_ENV=development nodemon index.js
[nodemon] 1.17.5
[nodemon] reading config ./nodemon.json
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 31462 to restart
[nodemon] ignoring: .git node_modules/**/node_modules
[nodemon] watching: /home/hunterhawley/server/**/* Intl
[nodemon] watching extensions: js,json
[nodemon] starting `node index.js`
[nodemon] forking
[nodemon] child pid: 31475
[nodemon] watching 37 files
/home/hunterhawley/node_modules/babel-core/lib/transformation/file/index.js:558
throw err;
^
SyntaxError: /home/hunterhawley/server/server.js: Unexpected token (164:8)
162 | const store = configureStore({
163 | app: {
> 164 | ...initialAppState,
| ^
165 | user: { ...req.session.user },
166 | },
167 | });
at Parser.pp$5.raise (/home/hunterhawley/node_modules/babylon/lib/index.js:4454:13)
at Parser.pp.unexpected (/home/hunterhawley/node_modules/babylon/lib/index.js:1761:8)
at Parser.pp$3.parseIdentifier (/home/hunterhawley/node_modules/babylon/lib/index.js:4332:10)
at Parser.pp$3.parsePropertyName (/home/hunterhawley/node_modules/babylon/lib/index.js:4156:96)
at Parser.pp$3.parseObj (/home/hunterhawley/node_modules/babylon/lib/index.js:4045:12)
at Parser.pp$3.parseExprAtom (/home/hunterhawley/node_modules/babylon/lib/index.js:3719:19)
at Parser.pp$3.parseExprSubscripts (/home/hunterhawley/node_modules/babylon/lib/index.js:3494:19)
at Parser.pp$3.parseMaybeUnary (/home/hunterhawley/node_modules/babylon/lib/index.js:3474:19)
at Parser.pp$3.parseExprOps (/home/hunterhawley/node_modules/babylon/lib/index.js:3404:19)
at Parser.pp$3.parseMaybeConditional (/home/hunterhawley/node_modules/babylon/lib/index.js:3381:19)
[nodemon] app crashed - waiting for file changes before starting...
所以在我看来,这是一个巴别塔问题。我可以附加我的index.js或任何其他文件,如果这能帮助您解决这个问题的话。谢谢大家!
编辑:我还想补充一点,安装过程(尽管需要做一些工作)进行得很顺利,我让MongoDB在另一个终端窗口中运行,这是这个应用程序所需要的。我也尝试过直接运行node index.js,但也没有成功
编辑2:下面是我在安装答案1中给出的插件时得到的信息
npm WARN babel-plugin-webpack-loaders@0.9.0 requires a peer of webpack@>=1.12.9 <3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN chunk-manifest-webpack-plugin@0.1.0 requires a peer of webpack@^1.4.0-beta6 but none is installed. You must install peer dependencies yourself.
npm WARN extract-text-webpack-plugin@1.0.1 requires a peer of webpack@^1.9.11 but none is installed. You must install peer dependencies yourself.
npm WARN webpack-dev-middleware@1.12.2 requires a peer of webpack@^1.0.0 || ^2.0.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN webpack-dev-server@2.11.2 requires a peer of webpack@^2.2.0 || ^3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN webpack-manifest-plugin@1.3.2 requires a peer of webpack@1 || 2 || 3 but none is installed. You must install peer dependencies yourself.
npm WARN @babel/plugin-proposal-object-rest-spread@7.3.4 requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN @babel/plugin-syntax-object-rest-spread@7.2.0 requires a peer of @babel/core@^7.0.0-0 but none is installed. You must install peer dependencies yourself.
npm WARN mern-starter@2.0.0 No repository field.
+ @babel/plugin-proposal-object-rest-spread@7.3.4
added 3 packages from 1 contributor and audited 20460 packages in 22.759s
最简单的解决方案是更新到节点v8.3或更高版本 “从Node v8.3开始,对象静止/展开是可用的,无需任何传输。” 如前所述,如果正在使用进行传输,则需要添加对的polyfill支持。在
.babelrc
文件中添加
"plugins": [
"@babel/plugin-proposal-object-rest-spread"
]
显然,你需要安装这个插件
npm i -D @babel/plugin-proposal-object-rest-spread
//OR
yarn add @babel/plugin-proposal-object-rest-spread --dev
注意:这需要“@babel/core”:“^7.0.0”
下面是如何为节点传输配置.babelrc
文件的示例
{
"presets": [
["@babel/env", {
"targets": {
"node": "current"
}
}]
],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
]
}
您还可以直接在Webpack config(Webpack.config.babel.js)中添加插件
我目前在Node/Babel项目中使用的特定包的示例(在package.json中),这可能有助于升级到Babel^7.0.0
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-object-rest-spread": "^7.3.1",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/preset-env": "^7.3.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.0.0",
"babel-loader": "^8.0.5",
"babel-plugin-module-resolver": "^3.2.0"
}
最简单的解决方案是更新到节点v8.3或更高版本 “从Node v8.3开始,对象静止/展开是可用的,无需任何传输。” 如前所述,如果正在使用进行传输,则需要添加对的polyfill支持。在
.babelrc
文件中添加
"plugins": [
"@babel/plugin-proposal-object-rest-spread"
]
显然,你需要安装这个插件
npm i -D @babel/plugin-proposal-object-rest-spread
//OR
yarn add @babel/plugin-proposal-object-rest-spread --dev
注意:这需要“@babel/core”:“^7.0.0”
下面是如何为节点传输配置.babelrc
文件的示例
{
"presets": [
["@babel/env", {
"targets": {
"node": "current"
}
}]
],
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-class-properties"
]
}
您还可以直接在Webpack config(Webpack.config.babel.js)中添加插件
我目前在Node/Babel项目中使用的特定包的示例(在package.json中),这可能有助于升级到Babel^7.0.0
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-object-rest-spread": "^7.3.1",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/preset-env": "^7.3.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.1",
"babel-jest": "^24.0.0",
"babel-loader": "^8.0.5",
"babel-plugin-module-resolver": "^3.2.0"
}
你的节点版本是什么?我试过11.10.1和6.0.0,我试过6.0.0的原因是因为和我一起/为我写这篇文章的人(我雇用了他,现在我自己也在试着学习节点)出于某种原因生产服务器运行在6.0.0。你的节点版本是什么?我试过11.10.1和6.0.0,我之所以尝试使用6.0.0,是因为与我一起/为我写这篇文章的人(我雇用了他,现在我自己也在尝试学习Node)出于某种原因,生产服务器运行在6.0.0中。当我安装插件时,我得到了一些东西,我将在我的回答中添加作为编辑哦,我可能猜到了你的问题。您需要运行一些接近
“@babel/core”:“^7.2.2”
。此外,我第一次运行时使用的是11.10.1,但没有通过。很抱歉,这可能真的很愚蠢,但我没有.babelrc文件。没有其他地方可以添加插件引用吗?我有一个webpack.config.babel.jsHmm如果您运行的是节点^11.0.0
,那么您不应该出现问题,因此可能存在其他问题,但使用您当前的配置,这应该可以工作,然后我们可以进一步排除故障。实际上,它的语法应该与.babelrc
文件中的语法几乎相同,但是你能将webpack.config.babel.js
添加到你的问题中吗?添加了@cantuket当我安装插件时,我会在我的回答中添加一些内容作为编辑哦,是的,我可能猜到了你的问题。您需要运行一些接近“@babel/core”:“^7.2.2”
。此外,我第一次运行时使用的是11.10.1,但没有通过。很抱歉,这可能真的很愚蠢,但我没有.babelrc文件。没有其他地方可以添加插件引用吗?我有一个webpack.config.babel.jsHmm如果您运行的是节点^11.0.0
,那么您不应该出现问题,因此可能存在其他问题,但使用您当前的配置,这应该可以工作,然后我们可以进一步排除故障。实际上,它的语法应该与.babelrc
文件中的语法几乎相同,但您能否将webpack.config.babel.js
添加到您的问题中?添加了@cantuket