问题描述
我有一个 apolloserver 项目给我带来麻烦,所以我认为我可能会更新它并在使用最新babel时遇到问题.我的" index.js"是:
require('dotenv').config() import {startServer} from './server' startServer()
当我运行时,我会得到错误
SyntaxError: Cannot use import statement outside a module
首先,我尝试做一些事情来说服TPTB*这是一个模块(没有成功).因此,我将" incort "更改为" 需要",这起作用了.
,但是现在我在其他文件中大约有两打" 进口"给我相同的错误.
*我确定问题的根源是我什至不确定在抱怨这个问题.我以为是Babel 7(因为我来自Babel 6,我不得不更改预设),但我不是100%确定的.
我发现解决方案的大部分内容似乎不适用于直节.像这样的人:
说它是通过添加" type =模块"来解决的,但这通常会在HTML中进行,我没有.我还尝试使用项目的旧预设:
"presets": ["es2015", "stage-2"], "plugins": []
但这给我带来了另一个错误:"错误:插件/预设文件不允许导出对象,只能函数."
这是我开始的依赖性:
"dependencies": { "@babel/polyfill": "^7.6.0", "apollo-link-error": "^1.1.12", "apollo-link-http": "^1.5.16", "apollo-server": "^2.9.6", "babel-preset-es2015": "^6.24.1",
推荐答案
验证您是否已安装了最新版本的node.js(或至少13.2.0+).然后执行以下操作之一,如文档中所述:
选项1
在最近的父package.json文件中,添加带有"module"值的顶级"type"字段.这将确保将所有.js和.mjs文件解释为ES模块.您可以将单个文件解释为 commonjs 通过使用.cjs扩展.
// package.json { "type": "module" }
选项2
用.mjs扩展名称命名文件.所有其他文件,例如.js将被解释为commonjs,如果未在type中定义type.
其他推荐答案
如果有人遇到了这个问题, testextript ,解决它的关键对我来说,正在改变
"target": "esnext", "module": "esnext",
to
"target": "esnext", "module": "commonjs",
在我的tsconfig.json中.我的印象" esnext"是"最好的",但这只是一个错误.
其他推荐答案
对于那些像我在阅读答案时一样困惑的人,在您的包装中,添加 "type": "module" 在上层如下所示:
{ "name": "my-app", "version": "0.0.0", "type": "module", "scripts": { ... }, ... }
问题描述
I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is:
require('dotenv').config() import {startServer} from './server' startServer()
And when I run it I get the error
SyntaxError: Cannot use import statement outside a module
First I tried doing things to convince TPTB* that this was a module (with no success). So I changed the "import" to a "require" and this worked.
But now I have about two dozen "imports" in other files giving me the same error.
*I'm sure the root of my problem is that I'm not even sure what's complaining about the issue. I sort of assumed it was Babel 7 (since I'm coming from Babel 6 and I had to change the presets) but I'm not 100% sure.
Most of what I've found for solutions don't seem to apply to straight Node. Like this one here:
ES6 module Import giving "Uncaught SyntaxError: Unexpected identifier"
Says it was resolved by adding "type=module" but this would typically go in the HTML, of which I have none. I've also tried using my project's old presets:
"presets": ["es2015", "stage-2"], "plugins": []
But that gets me another error: "Error: Plugin/Preset files are not allowed to export objects, only functions."
Here are the dependencies I started with:
"dependencies": { "@babel/polyfill": "^7.6.0", "apollo-link-error": "^1.1.12", "apollo-link-http": "^1.5.16", "apollo-server": "^2.9.6", "babel-preset-es2015": "^6.24.1",
推荐答案
Verify that you have the latest version of Node.js installed (or, at least 13.2.0+). Then do one of the following, as described in the documentation:
Option 1
In the nearest parent package.json file, add the top-level "type" field with a value of "module". This will ensure that all .js and .mjs files are interpreted as ES modules. You can interpret individual files as CommonJS by using the .cjs extension.
// package.json { "type": "module" }
Option 2
Explicitly name files with the .mjs extension. All other files, such as .js will be interpreted as CommonJS, which is the default if type is not defined in package.json.
其他推荐答案
If anyone is running into this issue with TypeScript, the key to solving it for me was changing
"target": "esnext", "module": "esnext",
to
"target": "esnext", "module": "commonjs",
In my tsconfig.json. I was under the impression "esnext" was the "best", but that was just a mistake.
其他推荐答案
For those who were as confused as I was when reading the answers, in your package.json file, add "type": "module" in the upper level as show below:
{ "name": "my-app", "version": "0.0.0", "type": "module", "scripts": { ... }, ... }