报错示例:

报错原因:

在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.的更多相关文章

  1. eclipse中的js文件报错的解决办法

    在使用别人的项目的时候,导入到eclipse中发现js文件报错,解决办法是关闭eclipse的js校验功能. 三个步骤: 1. 右键点击项目->properties->Validation ...

  2. eclipse 如何忽略js文件报错

    本文翻译自:http://stackoverflow.com/questions/10254922/jquery-mobile-1-1-0-min-triggers-errors-in-eclipse ...

  3. 导入项目后,js文件报错解决方法

    导入项目后,发现 js文件报错,但是js文件是从官网下载的.解决办法: 选中报错的js文件, 右键选择 MyEclipse-->Exclude From Validation : 然后继续右键执 ...

  4. eclipse忽略js文件报错

    eclipse中js文件报错的情况,或许大家早已习以为常了,那么有什么好的方法可以将其忽略掉呢?如果你也在寻找此问题,那么本文或许可以帮助到你忽略某个js文件报错的方法: Project Proper ...

  5. 第一次打开pycharm运行python文件报错”No Python interpreter selected“问题的解决办法

    前面没有细讲,这里细述一下安装pycharm后,第一次打开pycharm运行python文件报错"No Python interpreter selected"问题的解决办法. 出 ...

  6. 解决eclipse js文件报错(转)

    在我们将项目导入Eclipse后,配置好各种编译条件.加载好jar包.配置好tomcat后发现项目还是报错,(前提是项目本身并没有错误,而是我们在初次导入到Eclipse中的时候报错),那是什么原因引 ...

  7. 命令行运行py文件报错

    起因 今天用ubuntu 终端运行py文件报了个错,找不到模块? 我切换回pycharm中运行,运行一切正常 解决 在报错模块中,插入绝对路径 import sys sys.path.append(' ...

  8. gulp压缩js文件报错日志

    输出 gulp-uglify 压缩js文件时报错信息 gulp.task('es6', function () { return gulp.src('src/main/webapp/bower_com ...

  9. myeclipse中导入的js文件报错(出现红叉叉,提示语法错误)

    为了做一个页面特效,导入了一个jquery文件,怎想,myeclipse竟然报错说是语法错误,但是这个js文件我是从官网上下载的,不应该出错才对,百度谷歌之后终于找到了解决办法: 选中报错的js文件, ...

随机推荐

  1. Linux下非root用户运行Tomcat

    PS:Linux下使用非root用户运行tomcat的原因 由于项目需求,也由于root用户启动tomcat有一个严重的问题,那就是tomcat具有root权限. 这意味着你的任何一个页面脚本(htm ...

  2. “大地主”IPV6的邻居发现BD

    引入 因为当初设计IPv4的时候,没有考虑到网络发展的速度这么快,到今现在IPv4有很多不足,32位的 IPv4地址不够用,现在128位的IPv6能完全够用,据说可以地球上每一粒沙子都分配一个地址,而 ...

  3. caoz:数据分析这点事

    http://www.wocaoseo.com/thread-53-1-1.html 先声明一下,按照传统的定义,我还真不是数据分析高手,各种关联算法,只会最简单的一种(话说不少场合还算管用):各种挖 ...

  4. js 数组与字符串互相转换

    1.数组转字符串 arr.join() 2.字符串转数组 str.split(',')

  5. Visual Studio Installer闪退问题解决方法

    Visual Studio 2019安装推荐的方式是通过官方给的Installer进行的(2017也是同样方法),但是有时会出现在”即将完成…一切即将准备就绪“这个界面闪退的问题,导致软件的安装.卸载 ...

  6. C# .NET容器的源码

    这里有List<T>的源码http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs

  7. Funny Positive Sequence (思维+前缀)

    There are n integers a 1,a 2,…,a n-1,a n in the sequence A, the sum of these n integers is larger th ...

  8. SqlAnalyzer1.00源码

    SQL解析的夙愿今天终于完成,但限于SQL远超算术表达式,Json,XML等的复杂度,只是解析了一部分.形成普适性的SQL解析,仍需倾注精力. 代码下载:https://files.cnblogs.c ...

  9. Java通过http协议发送Get和Post请求

    https://blog.csdn.net/qq_32599479/article/details/94439131

  10. jenkins参数传递

    官方说明:https://wiki.jenkins.io/display/JENKINS/Parameterized+Build 1.添加参数传递 参数类型解析: 2.在 Execute shell ...