首页 > 解决方案 > 错误:不能在模块外使用导入?

问题描述

我从未使用过由例如create-react-appangular-cli创建的样板之外的模块,现在我试图在不使用它们的情况下模仿模块化,我得到了error: cannot use import outside a module

这是我的package.json文件:

  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-preset-env": "^1.7.0"
  }
}

我正在使用实时服务器为我的项目创建服务器,但我没有这样做:

应用程序.js

const x = 10;
export default x;

index.js

import x from './App' // can not use import outside a module

问题是我在这里缺少什么,创建模块我真正需要什么?

标签: javascriptnode.jsnpmmodule

解决方案


推荐阅读