简体   繁体   English

如何在 Chrome Devtools JS 控制台中从“http://bar.org/some-esm-module.js”中“导入 blah”?

[英]How do I `import blah from 'http://bar.org/some-esm-module.js'` in the Chrome Devtools JS console?

When I try to use an ESM import from the Chrome devtools / javascript console, it complains:当我尝试从 Chrome devtools / javascript 控制台使用 ESM 导入时,它抱怨:

> import Confetti from 'https://cdn.skypack.dev/canvas-confetti'

Uncaught SyntaxError: Cannot use import statement outside a module

Is there any way to convince the chrome console to execute as type='module' so I can use module statements?有什么方法可以说服 chrome 控制台以type='module'执行,以便我可以使用模块语句? Or another way to workaround this that still permits interactive imports of ESM modules for interactive development?或者另一种解决方法仍然允许交互式导入 ESM 模块以进行交互式开发?

I've been switching to heavily using ESM modules, in particular the large library of NPM ESM modules auto-converted by Skypack .我一直在大量使用 ESM 模块,特别是由Skypack自动转换的大型 NPM ESM 模块库。 This has worked great in static development, but I'm a heavy REPL / browser devtools user.这在静态开发中效果很好,但我是一个重度 REPL/浏览器 devtools 用户。

Even finding a third-party fakeImport(urlToESM) function I could use to load ESM modules would be a huge productivity improvement for me.即使找到一个我可以用来加载 ESM 模块的第三方fakeImport(urlToESM)函数对我来说也是一个巨大的生产力提升。

动态import()和顶级await应该可以工作:

const { default: Confetti } = await import('https://cdn.skypack.dev/canvas-confetti');

Dynamic import might come in handy.动态导入可能会派上用场。

import('/modules/my-module.js')
  .then((module) => {
    // Do something with the module.
});

You can use await with it.您可以将 await 与它一起使用。

let module = await import('/modules/my-module.js');

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用异步 ESM 导入获取 ESM 模块 - How do I get a ESM module using the Async ESM import 将 D3.js 7.0.0 与 Next.js 11.0.1 一起使用时,如何解决“[ERR_REQUIRE_ESM]:必须使用导入加载 ES 模块”的问题? - How do I resolve "[ERR_REQUIRE_ESM]: Must use import to load ES Module" when using D3.js 7.0.0 with Next.js 11.0.1? 如何将 HTTP url 与 Node ESM 模块加载器一起使用? - How do I use HTTP urls with Node ESM module loader? 在 Next.js ERR_REQUIRE_ESM 中导入 ES 模块 - Import ES module in Next.js ERR_REQUIRE_ESM 如何从intern.js测试中加载node.js http模块? - How do I load the node.js http module from within an intern.js test? Chrome DevTools 不显示 .js 文件 - Chrome DevTools do not show .js files 如何在NativeScript中使用核心JS导入和使用模块 - How do I import and use module using core JS in NativeScript 如何将JS源代码映射添加到Chrome devtools? - How to add JS source map into Chrome devtools? 如何将JSON文件数据导入JS文件并在JS控制台中输出JSON数据? - How do I import JSON file data into JS file and output the JSON data in JS console? Angular - 如何从 JS 包中导入模块 - Angular - How to import a module from a JS bundle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM