[英]When I wants to run my code in local server i always get this kinds of error “SyntaxError: Cannot use import statement outside a module”
I've got a web development project based on e-commerce but when I try to run my project on the local server I always getting this type of error.我有一个基于电子商务的 web 开发项目,但是当我尝试在本地服务器上运行我的项目时,我总是遇到这种类型的错误。 Don't find any solution yet.
还没有找到任何解决方案。
For better understanding, I added my server.js and package.json file in the post为了更好地理解,我在帖子中添加了我的 server.js 和 package.json 文件
Here it is sever.js这是sever.js
import express from 'express'; import data from './data'; const app = express(); app.get("/api/products", (req, res) => { res.send(data.products); }); app.listen (5000, () => {console.log("Server started at http://localhost:5000")})
In package.json在 package.json
{ "name": "web-development", "version": "1.0.0", "description": "nothing", "main": "nothing", "scripts": { "start": "nodemon --watch backend --exec babel-node backend/server.js" }, "repository": { "type": "git", "url": "" }, "author": "", "license": "ISC", "bugs": { "url": "" }, "homepage": "", "dependencies": { "express": "^4.17.1" }, "devDependencies": { "@babel/cli": "^7.10.5", "@babel/core": "^7.11.1", "@babel/node": "^7.10.5", "@babel/preset-env": "^7.11.0", "nodemon": "^2.0.4" } }
Update: As far console, I added "type": "module", package.json now I get another error更新:至于控制台,我添加了“类型”:“模块”,package.json 现在我得到另一个错误
Cannot GET /
不能获取 /
Showing in my browser在我的浏览器中显示
Node.js does not support ES6 modules import/export
syntax out of the box. Node.js 不支持开箱即用的 ES6 模块
import/export
语法。 Although you can enable it via --experimental-modules
flag .虽然您可以通过
--experimental-modules
标志启用它。
The project already uses Babel which transpile ES6 syntax to ES2015, but you need to add a few more scripts to package.json
该项目已经使用了将 ES6 语法转换为 ES2015 的Babel ,但您需要向
package.json
添加更多脚本
start
: Starts watching the file for changes. start
:开始观察文件的变化。 Then rebuilds the code and pipes it to babel-node
babel-node
build
: When you are ready to deploy the code for production, you build the ES2015 version of the code and you can run that. build
:当您准备好将代码部署到生产环境时,您可以构建 ES2015 版本的代码并运行它。 package.json
: package.json
:
"scripts": {
"start": "nodemon --watch backend --exec babel-node backend/server.js"
"build": "babel backend/server.js -o lib/server.js",
},
To build the production code, run npm run build
.要构建生产代码,请运行
npm run build
。 Once the code is built, you can then run node lib/server.js
构建代码后,您可以运行
node lib/server.js
lib/server.js
will look like: lib/server.js
看起来像:
Warning: To load an ES module, set "type": "module" in the package.json or use the.mjs extension.
I suggest you just editing your package.json like the following.我建议你像下面这样编辑你的 package.json。
{ "name": "web-development", "version": "1.0.0", "description": "nothing", "main": "nothing", "scripts": { "start": "nodemon --watch backend --exec babel-node backend/server.js" }, "repository": { "type": "git", "url": "" }, "author": "", "type": "module", "license": "ISC", "bugs": { "url": "" }, "homepage": "", "dependencies": { "express": "^4.17.1" }, "devDependencies": { "@babel/cli": "^7.10.5", "@babel/core": "^7.11.1", "@babel/node": "^7.10.5", "@babel/preset-env": "^7.11.0", "nodemon": "^2.0.4" } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.