简体   繁体   English

当我想在本地服务器上运行我的代码时,我总是会收到这种错误“SyntaxError: Cannot use import statement outside a module”

[英]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.

相关问题 当我尝试在我的 React 应用程序中运行 npm 测试时出现错误“SyntaxError: Cannot use import statement outside a module” - The error "SyntaxError: Cannot use import statement outside a module" appears when I try to run npm test in my react application 添加 {"type": "module"} 后出现“SyntaxError:Cannot use import statement outside a module” - I get "SyntaxError:Cannot use import statement outside a module", after adding {"type": "module"} 未捕获的语法错误:当我导出和导入 class 时,无法在模块外部使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module when I export and import a class 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? 运行编译的 Javascript 代码时,TypeScript 文件中出现“语法错误:无法在模块外使用导入语句”错误 - "SyntaxError: Cannot use import statement outside of a module" error in a TypeScript file when running compiled Javascript code Jest.js 和 Create-react-app:运行测试时出现“语法错误:无法在模块外使用导入语句” - Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test 导入模块时不能在模块外使用 import 语句 - Cannot use import statement outside a module when I import a module Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module 我不断收到未捕获的语法错误:无法在模块外使用 import use import 语句 - i keep getting uncaught syntaxError: cannot use import use import statement outside a module SwiperJS w/ SyntaxError:无法在模块错误之外使用导入语句 - SwiperJS w/ SyntaxError: Cannot use import statement outside a module error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM