I'm learning how to use VS Code Notebook API. I'm trying to build a single extension that contributes to notebooks and notebook renderers. While debugging the extension, I'm getting "Cannot Use Import Outside of a Module" error which is, as I suppose, connected with TSC/Webpack magic.
Steps to Reproduce
- Create a "New Notebook Renderer (TypeScript)" extension
yo code --extensionType=notebook
Open
src/extension/extension.tsAdd
import { TextEncoder } from 'util';Add
new TextEncoder();inactivatefunction bodyClick "Start Debugging"
As I understand, the error is caused by
import{createRequire as e}from"module";
which gets generated in out/extension/extension.js when there is a reference to a class from another module.
My Attempt to Solve the Problem
Open
webpack.config.jsRemove
experiments: {
outputModule: true,
},
- Replace
makeConfig(argv, { entry: './src/client/index.ts', out: './out/client/index.js', target: 'web', library: 'module' }),
with
makeConfig(argv, { entry: './src/client/index.ts', out: './out/client/index.js', target: 'web', library: 'commonjs' }),
Run
npm run compileClick "Start Debugging"
The previous error should disappear. Now there is another one
My further attempts to include var exports = {}; somewhere in build artifacts cause even more problems.
Do you know how to fix the error? I don't want to split the extension into two separate parts.


The main problem is that VS Extensions use Node.js and that requires (at least currently) code to be CommonJS. By adding an
importstatement, you are trying to use ESM syntax (CommonJS usesrequire). You have now three options to solve this:requireinstead ofimport.