节点/反应应用程序返回"SyntaxError: Unexpected token import"错误



我是一个新开发人员,刚进入 Node 和 React 所以对这个问题挠头。 我今天用谷歌搜索了 5 个多小时,并且已经用尽了这里和 GitHub 上提出的每一个解决方案,但无法解决我的问题,因此转向提出一个新问题。

我正在使用 Heroku 部署 Node/React 应用程序,但不断看到以下错误消息:

9:22:22 PM web.1 |  /Users/Captain_Kirk/Desktop/StarterApp/index.js:1
9:22:22 PM web.1 |  (function (exports, require, module, __filename, __dirname) { import React, { Component } from 'react';
9:22:22 PM web.1 |                                                                ^^^^^^
9:22:22 PM web.1 |  SyntaxError: Unexpected token import
9:22:22 PM web.1 |      at new Script (vm.js:51:7)
9:22:22 PM web.1 |      at createScript (vm.js:136:10)
9:22:22 PM web.1 |      at Object.runInThisContext (vm.js:197:10)
9:22:22 PM web.1 |      at Module._compile (module.js:613:28)
9:22:22 PM web.1 |      at Object.Module._extensions..js (module.js:660:10)
9:22:22 PM web.1 |      at Module.load (module.js:561:32)
9:22:22 PM web.1 |      at tryModuleLoad (module.js:501:12)
9:22:22 PM web.1 |      at Function.Module._load (module.js:493:3)
9:22:22 PM web.1 |      at Function.Module.runMain (module.js:690:10)
9:22:22 PM web.1 |      at startup (bootstrap_node.js:194:16)
9:22:22 PM web.1 Exited with exit code 1

这是我的package.json文件:

{
"name": "StarterApp",
"version": "1.0.0",
"description": "test app",
"engines": {
"node": "9.8.0"
},
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"fs-extra": "^5.0.0",
"ganache-cli": "^6.1.0",
"mocha": "^5.0.5",
"next": "^4.1.4",
"next-routes": "^1.4.1",
"npm": "^6.1.0",
"react": "^16.3.1",
"react-dom": "^16.3.1",
"semantic-ui-css": "^2.3.1",
"semantic-ui-react": "^0.79.1",
"solc": "^0.4.21",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^1.0.0-beta.26",
"webpack-cli": "^2.0.13"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"webpack": "^3.12.0"
},
"directories": {
"test": "test"
}
}

当我运行"npm install"时,我看到以下警告:

npm WARN deprecated babel-preset-es2015@6.24.1: 🙌  Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update! 

^ 我似乎已经有了正确的预设,所以不确定为什么会出现此警告。

其他 npm 警告:

npm WARN deprecated nomnom@1.8.1: Package no longer supported. Contact support@npmjs.com for more info.
npm WARN ajv-keywords@3.2.0 requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN webpack-cli@2.0.13 requires a peer of webpack@^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN kickstart@1.0.0 No repository field.

我在这里做错了什么?

由于您使用的是 React,因此建议您在开发依赖项中添加 'babel-preset-react'。

您可以在此处阅读有关该模块的更多信息:https://www.npmjs.com/package/babel-preset-react

Node 不允许使用 Express 关键字import而是使用require作为import的替代品。

希望它可能对你有用。

相关内容

最新更新