English 中文(简体)
2. 先天: 模块外不得使用进口说明
原标题:SyntaxError: Cannot use import statement outside a module

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()

而且,当我管理时,我就犯了错误。

2. 先天: 模块外不得使用进口说明

首先,我试图做事,说服TPTB*相信这是一个模块(没有成功)。 因此,我把“import”改为“require,这项工作已经完成。

但现在,在其他文件中,我有大约20个“<>进口”,给我同样的错误。

* 我确信我问题的根源是,我甚至不敢肯定对这个问题的抱怨。 我的假设是Babel 7(因为Im来自Babel 6,我不得不改变序号),但我肯定不是100%。

我发现的多数解决办法似乎不适用于直线的诺德。 同这里一样:

< 进口模块,“无光谱识别符号”

之所以解决这一难题,是增加“类型=模块”,但这通常属于超文本,我没有。 我也利用我的项目的老序:

"presets": ["es2015", "stage-2"],
"plugins": []

但是,这又给我带来另一个错误:“错误:不允许Plugin/Preset文档出口物体,只能是功能”。

这里是我从以下几个方面开始的附属因素:

"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",
最佳回答
问题回答

如果有人在Typegust上处理这个问题,那么解决该问题的关键是改变。

    "target": "esnext",
    "module": "esnext",

纽约总部

    "target": "esnext",
    "module": "commonjs",

在我的<代码>中,sconfig.json。 我的印象是“esnext”是“最佳”,但这只是一个错误。

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": { ...
  },
  ...
}

http://nodejs.org/api/esm.html#esm_code_import_code_statements”rel=“noreferer”

import statements are permitted only in ES modules. For similar functionality in CommonJS, see import().

为使Node.js将你的档案作为经济单元处理,你需要(<>Enabling):

  • add "type": "module" to package.json
  • add "--experimental-modules" flag to the Node.js call

我也谈到同样的问题,更糟糕的是: 我既需要“进口”,又需要“需要”

  1. Some newer ES6 modules works only with import.
  2. Some CommonJS works with require.

对我来说,这是一件事:

  1. 将贵格卷宗改为 在其他答复中建议

  2. “要求”与ES6模块没有定义,因此,你可以这样界定:

    import { createRequire } from  module 
    const require = createRequire(import.meta.url);
    

    现在的要求可以惯常的方式使用。

  3. 用于ES6模块和require用于共同JS。

一些有用的链接:Node.js s own documentation. .difference between import and required .

我接受所有方法,但无所作为。

我从Gite Hub那里获得一句话。

为了使用新版本的进口品,我安装了以下包裹。

1. npm i typescript --save-dev

2. npm i ts-node --save-dev

www.un.org/Depts/DGACM/index_spanish.htm Won t 要求类型:包装模块。

例如,

{
  "name": "my-app",
  "version": "0.0.1",
  "description": "",
  "scripts": {

  },
  "dependencies": {
    "knex": "^0.16.3",
    "pg": "^7.9.0",
    "ts-node": "^8.1.0",
    "typescript": "^3.3.4000"
  }
}

Node v14.16.0
For those who ve tried .mjs and got:

Aviator@AW:/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex$ node just_js.mjs
file:///mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex/just_js.mjs:3
import fetch from "node-fetch";
       ^^^^^
    
SyntaxError: Unexpected identifier

and who ve tried import fetch from "node-fetch";
and who ve tried const fetch = require( node-fetch );

Aviator@AW:/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex$ node just_js.js
(node:4899) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex/just_js.js:3
import fetch from "node-fetch";
^^^^^^

SyntaxError: Cannot use import statement outside a module  

和谁尝试了<代码>”类型:“模块”/代码。 json,但继续看到错误,

{
  "name": "test",
  "version": "1.0.0",
  "description": "to get fetch working",
  "main": "just_js.js",
  "type": "module",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "MIT"
}

我能够转向没有问题的轴心。

import axios from axios ; <-- put at top of file.
Example:

axios.get( https://www.w3schools.com/xml/note.xml ).then(resp => {
    console.log(resp.data);
});

我发现,2020年这一联系的答复有助于回答这一问题,并告诉你,我们这样做:

Using Node.js 要求诉ES6进出口

Here s an excerpt:

Update 2020

Since Node v12, support for ES modules is enabled by default, but it s still experimental at the time of writing this. Files including node modules must either end in .mjs or the nearest package.json file must contain "type": "module". The Node documentation has a ton more information, also about interop between CommonJS and ES modules."

步骤1

yarn add esm

npm i esm --save

步骤2

页: 1

"scripts": {
  "start": "node -r esm src/index.js",
}

步骤3

nodemon --exec npm start

I m new to Node.js, and I got the same issue for the AWS Lambda function (using Node.js) while fixing it.

I found some of the differences between CommonJS and ES6 JavaScript:

<><>><>><>><>>>><>>>>>>

  • 添加“类型”: 包装,json文档

  • 利用“进口”从平衡中使用。

    <>Example: 进口jwt_decode from jwt-decode

  • Lam卜达手法法典应当像这样界定。

    “出口品:手勒=yn(喷口)=>{}”

<><>>>CommonJS:>>>

  • Don t Add “type”:"module” in Package.json file

  • 利用“需要”进行校准。

    <>Example: const jwt_decode = 要求(“jwt-decode”);

  • lam卜达手法法典应如此界定:

    “出口冲锋” = yn;{}”

进口使用如下。

  1. Rename the .js file to .mjs
  2. In package.json file, add {type:module}

This error also comes when you run the command

node filename.ts

而不是

node filename.js

Simply put, with the node command we will have to run the JavaScript file (filename.js) 而不是 the TypeScript file unless we are using a package like ts-node.

我在人工升级后,在我的 工作空间

transform: {
   ^.+\.(ts|js|html)$ :  jest-preset-angular ,
},

to

transform: {
   ^.+\.(ts|mjs|js|html)$ :  jest-preset-angular ,
},

If you are using vite - react .js application with Speedy Web Compiler (SWC) and you tried to import the various methods of jest testing library ({import { describe, expect, test } from "@jest/globals";), all your test will run individually and outside the module. Hence you might get the error as :

2. 先天: 模块外不得使用进口说明

由于你正在使用SWC,巴贝尔教派赢得了帮助。 因此,可以通过简单地给予发展中国家依赖来解决:

www.un.org/Depts/DGACM/index_spanish.htm

后来删除了进口说明、使用方法(登记、测试、预期在除异之后,在所有人之前,在每一方失败之前,会scribe、scribe、 fit、 j、 pending等。 由于被作为全球人对待,因此,在肉类、Xit、Xtest上直接使用。 这里的所有链接和其他文件! @types/jest

测试你的总和。

const sum = (val1, val2) => {
  return val1 + val2;
};

describe("sum module", () => {
  test("adds 1 + 2 to equal 3", () => {
    expect(sum(1, 2)).toBe(3);
  });
});

仅添加-presets @babel/preset-env

例如,

babel-node --trace-deprecation --presets  @babel/preset-env  ./yourscript.js

babel.config.js

module.exports = {
  presets: [ @babel/preset-env ],
};

To make your import work and avoid other issues, like modules not working in Node.js, just note that:

有了ES6模块,你还没有进口目录。 你的进口应当这样看:

import fs from  ./../node_modules/file-system/file-system.js 

对于由于网络功能中的这一错误而进入这一深层的人来说,甚至在添加了<条码>型号”之后:包装中的“模块netlification.toml,以使用 esbuild 。 由于浮工支持ES6,它将发挥作用。

[functions]
  node_bundler = "esbuild"

Reference: https://docs.netlify.com/functions/build-with-javascript/#automated-dependency-bundling

文件令人困惑。 我利用Node.js在我的电脑中履行一些当地任务。

如果我想使用test.js,请看一下我的旧文字。

import something from "./mylocalECMAmodule";

它将造成这样的错误:

(node:16012) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
SyntaxError: Cannot use import statement outside a module
...

这是not一个模块错误,但是Node.js错误。 禁止在模块之外装货。

为了确定这一点,仅将你的旧字母test.js改为test.mjs

所有这一切都是如此。

在我进行移徙时,我有这个问题。

它是一个问题。

在这方面,我是如何解决的:

页: 1

npm install @babel/register

增 编

require("@babel/register")

在我的上,我提交了文件。

And go ahead to run my sequelize migrate. This is applicable to other things, apart from sequelize.

transpiling

我的解决方案是把巴贝尔-诺德的道路纳入其中,同时进行以下无休止:

nodemon node_modules/.bin/babel-node index.js

页: 1 原文:

debug: nodemon node_modules/.bin/babel-node index.js

注:我的条目是index.js。 改为your 入境档案 (有app.js/server.js)。

  1. I had the same problem when I started to use Babel... But later, I had a solution... I haven t had the problem any more so far... Currently, Node.js v12.14.1, "@babel/node": "^7.8.4", I use babel-node and nodemon to execute (Node.js is fine as well..)
  2. package.json: "start": "nodemon --exec babel-node server.js "debug": "babel-node debug server.js"!! Note: server.js is my entry file, and you can use yours.
  3. launch.json. When you debug, you also need to configure your launch.json file "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node"!! Note: plus runtimeExecutable into the configuration.
  4. Of course, with babel-node, you also normally need and edit another file, such as the babel.config.js/.babelrc file

In case you re running nodemon for the Node.js version 12, use this command.

server.js is the "main" within Package.json file,取而代之的是 Package.json file:

nodemon --experimental-modules server.js

Wrong MIME-Type for JavaScript Module Files

The common source of the problem is the MIME-type for "Module" type JavaScript files is not recognized as a "module" type by the server, the client, or the ECMAScript engine that process or deliver these files.

The problem is the developers of Module JavaScript files incorrectly associated Modules with a new ".mjs" (.js) extension, but then assigned it a MIME-type server type of "text/javascript". This means both .js and .mjs types are the same. In fact the new type for .js JavaScript files has also changed to "application/javascript", further confusing the issue. So Module JavaScript files are not being recognized by any of these systems, regardless of Node.js or Babel file processing systems in development.

主要问题是,大多数服务器或客户(现代超文本5浏览器)都知道这一新的“模块”式 Java本。 换言之,他们无法知道什么? 模块文件类型与Java 字体不同!

因此,你收到你的答复,因为 Java本发动机说,它需要知道档案是否是贾瓦文的单元。

服务器或客户的唯一解决办法是 改变其服务器或浏览器,以提供一种新的气象类型,使ES6支持模块文件,该模型有.mjs的延伸。 现在,这样做的唯一途径是,为任何有<代码>>>mjs的文档制作一个“module的<>>>>,将其在模块Java 文档上的档案延伸改为“.mjs”,或者在任何外部<代码>上添加一个带有“密码”的“module”>的超文本标签; 编号和“>;要素”用于下载你的外部<代码>。 Javacast模块文档。

一旦你将浏览器或 Java式发动机 into为接受新的模块文档类型,他们将开始在联合材料发动机或Node.js系统中进行包装。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签