[vscode直接运行js文件报错]: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
报错示例:
报错原因:
在vscode里面编写了一段js代码,使用了
import
来引入一个函数并调用
代码复现
// inherit() returns a newly created object that inherits properties from the
// prototype object p. It uses the ECMAScript 5 function Object.create() if
// it is defined, and otherwise falls back to an older technique.
export function inherit(p) {
if (p == null) throw TypeError(); // p must be a non-null object
if (Object.create) // If Object.create() is defined...
return Object.create(p); // then just use it.
var t = typeof p; // Otherwise do some more type checking
if (t !== "object" && t !== "function") throw TypeError();
function f() {}; // Define a dummy constructor function.
f.prototype = p; // Set its prototype property to p.
return new f(); // Use f() to create an "heir" of p.
}
//报错的罪魁祸首
import {inherit} from "./inherit.js"
var p = {
x:1.0,
y:1.0,
get r(){
return Math.sqrt(this.x**2+this.y**2)
},
set r(newValue){
let oldValue = Math.sqrt(this.x**2+this.y**2)
let ratio = newValue/oldValue
this.x *= ratio
this.y *= ratio
}
}
var q = inherit(p)
q.x = 3
q.y = 4
console.log(q.r)
解决方案
在package.json里面加入type:"module"
字段
npm init -y
//package.json
{
"name": "javascript-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
//新增以下这行
"type": "module",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
成功运行结果
[vscode直接运行js文件报错]: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.的更多相关文章
- eclipse中的js文件报错的解决办法
在使用别人的项目的时候,导入到eclipse中发现js文件报错,解决办法是关闭eclipse的js校验功能. 三个步骤: 1. 右键点击项目->properties->Validation ...
- eclipse 如何忽略js文件报错
本文翻译自:http://stackoverflow.com/questions/10254922/jquery-mobile-1-1-0-min-triggers-errors-in-eclipse ...
- 导入项目后,js文件报错解决方法
导入项目后,发现 js文件报错,但是js文件是从官网下载的.解决办法: 选中报错的js文件, 右键选择 MyEclipse-->Exclude From Validation : 然后继续右键执 ...
- eclipse忽略js文件报错
eclipse中js文件报错的情况,或许大家早已习以为常了,那么有什么好的方法可以将其忽略掉呢?如果你也在寻找此问题,那么本文或许可以帮助到你忽略某个js文件报错的方法: Project Proper ...
- 第一次打开pycharm运行python文件报错”No Python interpreter selected“问题的解决办法
前面没有细讲,这里细述一下安装pycharm后,第一次打开pycharm运行python文件报错"No Python interpreter selected"问题的解决办法. 出 ...
- 解决eclipse js文件报错(转)
在我们将项目导入Eclipse后,配置好各种编译条件.加载好jar包.配置好tomcat后发现项目还是报错,(前提是项目本身并没有错误,而是我们在初次导入到Eclipse中的时候报错),那是什么原因引 ...
- 命令行运行py文件报错
起因 今天用ubuntu 终端运行py文件报了个错,找不到模块? 我切换回pycharm中运行,运行一切正常 解决 在报错模块中,插入绝对路径 import sys sys.path.append(' ...
- gulp压缩js文件报错日志
输出 gulp-uglify 压缩js文件时报错信息 gulp.task('es6', function () { return gulp.src('src/main/webapp/bower_com ...
- myeclipse中导入的js文件报错(出现红叉叉,提示语法错误)
为了做一个页面特效,导入了一个jquery文件,怎想,myeclipse竟然报错说是语法错误,但是这个js文件我是从官网上下载的,不应该出错才对,百度谷歌之后终于找到了解决办法: 选中报错的js文件, ...
随机推荐
- Asp.NetCore 3.1 使用AutoMapper自动映射转换实体 DTO,Data2ViewModel
1:什么是AutoMapper? 下面为AutoMapper官方的解释: AutoMapper是一个对象-对象映射器.对象-对象映射通过将一种类型的输入对象转换为另一种类型的输出对象来工作. 使Aut ...
- git ssh配置
SSH KEY的配置 生成 SSH KEY ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 文件在哪里生成的 C:\用户 ...
- 我们真的需要JWT吗?
JWT(JSON Web Token)是目前最流行的认证方案之一.博客园.各种技术公众号隔三差五就会推一篇JWT相关的文章,真的多如牛毛.但我对JWT有点困惑,今天写出来跟大家探讨探讨,不要喷哈. J ...
- Android Weekly Notes Issue #428
Android Weekly Issue #428 Kotlin Flow Retry Operator with Exponential Backoff Delay 这是讲协程Flow系列文章中的一 ...
- sdf文件可以通过database net4工具升级版本
用database .net4工具打开数据库后,右键数据库->数据库工具->upgrade to->to 4.0 or to 3.5; 可以用来判断数据库版本及是否要升级.
- tars
动手实践Tars服务的搭建 https://blog.csdn.net/sunshine1314/article/details/81151080 Tars-Go 服务 Hello World——从 ...
- C#显示百度地图API
http://dev.baidu.com/wiki/static/map/API/examples/?v=1.3&2_0#2&0 太原市的经纬度:112.596, 37.884 北京市 ...
- zabbix-4.0-监控服务器的ping告警设置
问题:一直在困惑如果一台服务器的网络发生故障或者断开时,怎么第一时间发现并去排查. 思路:利用zabbix平台监控服务器,监控ping这一项,设置一个报警,并使用脚本去提醒与通知,可使用邮件报警/短信 ...
- HDU-Tick and Tick
The three hands of the clock are rotating every second and meeting each other many times everyday. F ...
- CentOS yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
在虚拟机上安装完CentOS6.5之后,首次使用时yum命令安装软件时,出现一堆的” Errno 14 Couldn't resolve host”这个问题. 上网上查了半天,很多都说在/etc/re ...