I am trying to create an extension for VS Code. The purpose of the extension is to add a command in the context menu, so when the user right-clicks on a folder with a certain relative path the option shows up in the menu. If the user clicks on the option in the menu it starts a new terminal and runs a specific command.
I am having trouble importing a package in my extension.js file.
I tried two different approaches. First I tried using the import-keyword:
import { findUpSync } from 'find-up';
function detectPackageManager(cwd) {
if (findUpSync('yarn.lock', { cwd })) return 'yarn';
if (findUpSync('package-lock.json', { cwd })) return 'npm';
return 'yarn';
}
which resulted in the following error: Cannot use import statement outside a module.
Then I tried using the require-keyword:
const findUp = require('find-up');
function detectPackageManager(cwd) {
if (findUp.findUpSync('yarn.lock', { cwd })) return 'yarn';
if (findUp.findUpSync('package-lock.json', { cwd })) return 'npm';
return 'yarn';
}
which resulted in the following error:
'undefined_publisher.runfromcontextmenu' failed: require() of ES Module /home/.../node_modules/find-up/index.js from /home/.../extension.js not supported. Instead change the require of index.js in /home/.../extension.js to a dynamic import() which is available in all CommonJS modules..