我得到"意外的令牌导入"当我试图运行我的反应应用程序时
我得到的错误
User:my-version username$ node build
User:my-version username$ node app.js
/Users/Hindreen/Documents/workspace/apps/test/app.js:1
(function (exports, require, module, __filename, __dirname) { import React
from 'react';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:588:28)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
User:test username$
app.js
import React from 'react';
import ReactDOM from 'react-dom';
class Application extends React.Component {
render() {
return (
<div className="container">
<h1>Hello From React</h1>
</div>
);
}
}
build.js
var fs = require("fs");
var browserify = require("browserify");
var babelify = require("babelify");
browserify({ debug: true })
.transform(babelify)
.require("./app.js", { entry: true })
.bundle()
.on("error", function (err) { console.log("Error: " + err.message); })
.pipe(fs.createWriteStream("bundle.js"));
.babelrc
{
"presets": ["env", "react"],
"plugins": ["transform-es2015-modules-amd"]
}
package.json devDependencies
{
"devDependencies":{
"babel-plugin-transform-es2015-modules-amd": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.24.1",
"babelify": "^7.3.0",
"browserify": "^13.3.0",
"react": "^15.0.2",
"react-dom": "^15.0.2"
}
}
我感谢您的帮助,提前致谢,如果我的问题不明确,请告诉我。
答案 0 :(得分:0)
const React = require('react'),
ReactDOM = require('react-dom');
如果你用ES6-syntax =&gt;写的话安装和使用babel
答案 1 :(得分:0)
-> User:my-version username$ node app.js
除非您想运行代码服务器端,否则这不是引导“构建”代码的适当方式。
请参阅以下内容模仿您的设置:https://repl.it/repls/RemoteJovialSignature (它实际上并没有运行,它仅用于查看目的)。
本地运行的步骤:
node build
- 构建bundle.js index.html
答案 2 :(得分:0)
我刚刚解决了将我的脚本文件text.js
从Unicode保存回UTF的问题。可能Unicode中的单引号(')未被识别。
它失败了
var mysql = require('mysql');