SyntaxError: Cannot use import statement outside a module

这篇具有很好参考价值的文章主要介绍了SyntaxError: Cannot use import statement outside a module。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

(node:24300) 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)

import fs from 'fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
[nodemon] app crashed - waiting for file changes before starting...

如运行node项目时出现如下错误,提示和网上的结果是再package.json 文件里加上"type": "module", 因为node 默认是CommonJS 模块管理的, 然后后等你使用了type: module时,它又出现

require is not defined

node 默认的require导入格式被type: module冲突了

所以 ,同时使用 importrequire 两种模块管理模式的正确方式应该是使用,Babel 插件转换

https://www.ruanyifeng.com/blog/2016/01/babel.html 阮一峰老师的博客babel入门教程

package.json 文件中添加如下依赖

 "@babel/core": "^7.19.3",
 "@babel/plugin-proposal-decorators": "^7.19.3",
  "@babel/register": "^7.18.9",
  "babel-preset-env": "^1.7.0",

.babelrc 文件中添加如下规则

{
  "presets": [
    ["env", {"targets": {"node": "current"}}]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

然后遇到什么奇奇怪怪的问题就在网上使劲 搜索,因为历史总是如此相似。文章来源地址https://www.toymoban.com/news/detail-531479.html

到了这里,关于SyntaxError: Cannot use import statement outside a module的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result

    看看自己的java代码里的 sql语句的 select是不是写错了!   我把select写成了 selsct !!!!  

    2024年02月11日
    浏览(37)
  • Caused by: java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produ

    在用Spring Boot JPA的时候, 导致异常: Caused by: java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result sets. 解决方法: 在 @Query 上加上 @Modifying ,表示不需要返回值 这是因为,根据Spring data jpa官网 Doing so triggers the query annotated to the method as an updating query in

    2024年02月12日
    浏览(42)
  • Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0

    项目: taro3+vue3 描述:运行时警告 Deprecation Warning: Using / for division outside of calc() is deprecated and will be removed in Dart Sass 2.0.0. 原因:sass版本的问题, 换成calc 方法一:sass-migrator 全局安装 sass-migrator, 在node_modules/.bin中运行 sass-migrator division ./node_modules/taro-ui-vue3/dist/style/components/*.

    2024年02月11日
    浏览(31)
  • go mod init 在初始化时出现 cannot determine module path for source directory (outside GOPATH,module path)

    新创建的golang项目,使用 go mod init 命令时出现 cannot determine module path for source directory xxxxxxx (outside GOPATH, module path must be specified) 这是因为go mod init 初始化项目时,需要定义一个 module ,当打开一个 go.mod 文件,就会发现第一行就有 因此,在执行 go mod init 时需要定义 module,如:

    2024年02月13日
    浏览(36)
  • 解决 ImportError: cannot import name ‘metadata‘

    报错截图: 在导入 nptdms 时 出现的错误 python  版本为 3.6.8 nptdms 版本为 1.6.2 解决方法为 找到  version.py  文件位置,修改

    2024年01月19日
    浏览(39)
  • cannot import name ‘_compare_version‘ from ‘torchmetrics.utilities.imports‘

    Traceback (most recent call last): File “/scratch/AzureNfsServer_INPUT1/vc_data/users/willing/home/mQG/src/1_train.py”, line 14, in import pytorch_lightning as pl File “/home/aiscuser/.conda/envs/willing/lib/python3.9/site-packages/pytorch_lightning/ init .py”, line 34, in from pytorch_lightning.callbacks import Callback # noqa: E402 File “/home/ai

    2024年01月16日
    浏览(32)
  • cannot import name ‘webdriver‘ from ‘selenium‘

    1、检查是否安装了selenium  如果没有安装selenium,直接 pip install selenium 即可 2、检查现在所运行的文件名是否有selenium 因为webdriver是从selenium中导入的,如果文件名中有selenium,那么它在调用的时候就会默认先调用你的文件,真正的webdriver包就导入不了,识别不出来。    

    2024年02月15日
    浏览(37)
  • java import、package与php的namespace和use、requie实质

    首先,java的工作原理是,首先去java的环境变量classpath或者你当前编译目录下去找有没有你需要的类 import、package 后跟着的其实不是文件的真实路径,事实上,它们不需要按照名称组织文件夹,你把他们统统放在一个目录下也可以正常编译运行。将其按照文件路径组织仅仅是

    2024年02月11日
    浏览(33)
  • 【已解决cannot import name ‘OrderedDict‘ from ‘typing‘】

    在import tensorflow as tf 时报错no mould tensorflow,自然而然想到pip一下,但是pip一直显示成功,并且不会报任何错 由于我下载的是python3.7版本,tensorflow可能需要下载补丁包,故在网上查询解决方案: 在下载后,将该目录下的function_type.py中的 改为 此时依旧会报错 cannot import name ‘

    2024年02月16日
    浏览(32)
  • Eslint配置 Must use import to load ES Module(已解决)

    最近在配置前端项目时,eslint经常会碰到各种报错(灰常头疼~) Syntax Error Error No ESLint configuration found. Syntax Error: Error: D:dmqdmq-ui.eslintrc.js: Environment key “es2021” is unknown at Array.forEach () error in ./src/main.js Syntax Error: Error: Cannot find module ‘@vue/cli-plugin-babel/preset’ from ‘D:dmqdmq

    2024年02月04日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包