Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot resolve dependency 'vscode' #3034

Closed
Tracked by #4042
JonathanTroyer opened this issue May 15, 2019 · 11 comments
Closed
Tracked by #4042

Cannot resolve dependency 'vscode' #3034

JonathanTroyer opened this issue May 15, 2019 · 11 comments

Comments

@JonathanTroyer
Copy link

JonathanTroyer commented May 15, 2019

🐛 bug report

Parcel fails on import * as code from 'vscode';. It also fails if I import directly, i.e. import { ExtensionContext } from 'vscode';

🎛 Configuration (.babelrc, package.json, cli command)

Package.json

{
    "name": "Converter",
    "publisher": "me",
    "displayName": "ASP Convert",
    "description": "Assists with converting ASP pages to HTML5 standards",
    "license": "MIT",
    "version": "1.0.1",
    "engines": {
        "vscode": "^1.33.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onCommand:extension.aspconvert",
        "onLanguage:asp"
    ],
    "main": "./dist/extension.js",
    "contributes": {
        "commands": [
            {
                "command": "extension.abconvert",
                "title": "ASP Convert"
            }
        ]
    },
    "scripts": {
        "vscode:prepublish": "yarn run compile",
        "compile": "parcel build ./src/extension.ts --log-level 4",
        "watch": "parcel ./src/extension.ts",
        "postinstall": "node ./node_modules/vscode/bin/install",
        "test": "yarn run compile && node ./node_modules/vscode/bin/test",
        "package": "npx vsce package"
    },
    "devDependencies": {
        "@types/mocha": "^2.2.42",
        "@types/node": "^10.12.21",
        "@types/vnu-jar": "^17.11.0",
        "parcel-bundler": "^1.12.3",
        "tslint": "^5.12.1",
        "typescript": "^3.4.5",
        "vscode": "^1.1.28"
    },
    "dependencies": {
        "vnu-jar": "^18.11.5"
    }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "out",
        "lib": [
            "es6",
            "esnext.array"
        ],
        "sourceMap": true,
        "rootDir": "src",
        "strict": true   /* enable all strict type-checking options */
        /* Additional Checks */
        // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
        // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
        // "noUnusedParameters": true,  /* Report errors on unused parameters. */
    },
    "exclude": [
        "node_modules",
        ".vscode-test"
    ]
}

🤔 Expected Behavior

Parcel should bundle the VSCode extension

😯 Current Behavior

[4:57:22 PM]: Building...
[4:57:22 PM]: Building extension.ts...
[4:57:23 PM]: Building rules.ts...
[4:57:23 PM]: 🚨  /Users/jonathantroyer/Desktop/asp-converter/src/extension.ts:7:25: Cannot resolve dependency 'vscode'
[4:57:23 PM]:     at Resolver.resolve (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Resolver.js:71:17)
    at async Bundler.resolveAsset (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Bundler.js:433:18)
    at async Bundler.resolveDep (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Bundler.js:484:14)
    at async /Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Bundler.js:608:26
    at async Promise.all (index 3)
    at async Bundler.loadAsset (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Bundler.js:599:21)
    at async Bundler.processAsset (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/Bundler.js:557:5)
    at async PromiseQueue._runJob (/Users/jonathantroyer/Desktop/asp-converter/node_modules/parcel-bundler/src/utils/PromiseQueue.js:48:7)

💁 Possible Solution

Unsure

🔦 Context

I'm currently trying to bundle a VSCode extension.

💻 Code Sample

import { ExtensionContext, commands, window, workspace } from "vscode";

export function activate(context: ExtensionContext) {
    context.subscriptions.push(
        commands.registerCommand('extension.aspconvert', _ => {
            let currentEditor = window.activeTextEditor;
            if (currentEditor !== undefined) {
                window.showInformationMessage('Hello World!!');
            } else {
                window.showInformationMessage('No file active');
            }
        })
    );
}

export function deactivate() { }

🌍 Your Environment

Software Version(s)
Parcel 1.12.3
Node 12.1.0
npm/Yarn 1.16.0
Operating System macOS Mojave 10.14.4 (18E226)
@mischnic
Copy link
Member

VSCode specifies no main field so the resolution of import "vscode" falls back to node_modules/vscode/index.js, which doesn't exist either.

Running require("vscode"); in plain node also throws: "Error: Cannot find module 'vscode'"

I'm guessing VSCode somehow injects the real module during runtime?
If that's the case, you should run parcel build index.ts -t node which leaves the require("vscode") import and doesn't try to resolve it

@JonathanTroyer
Copy link
Author

Changing the target to node worked! Thanks a ton, I'll mark this as closed. Hopefully people bundling VSCode extensions in the future can run across this.

One note, the flag is --target (not just -t).

@mischnic
Copy link
Member

Great!

-t should work as well though, seems to be missing in the documentation.

.option(
'-t, --target [target]',
'set the runtime environment, either "node", "browser" or "electron". defaults to "browser"',
/^(node|browser|electron)$/
)

@Abyrd9
Copy link

Abyrd9 commented Dec 21, 2019

I am having a similar problem. Changing the target to node leads to a successful build since it's no longer bundling dependencies, but I have other dependencies besides vscode that I do need bundled. Still trying to figure out a solution if anyone runs across something.

@ctf0
Copy link

ctf0 commented Feb 9, 2020

have the same issue as @Abyrd9 , any solution for that ?

@vudzero
Copy link

vudzero commented Aug 2, 2020

Having same issue as @Abyrd9 , trying to migrate to Parcel2 to see if it can fix the issue...

@jonas-k
Copy link

jonas-k commented Nov 10, 2020

Could this issue be reopened since excluding all node modules is not really a fix? I also ran into this problem and couldn't circumvent it by using includeNodeModules: { "vscode": false }.

@sketchbuch
Copy link

so what do you do if you are bundling a vscode extension (target=node) but need dependencies (except vscode) in the bundle

@mischnic
Copy link
Member

mischnic commented Dec 2, 2020

With Parcel 2, you can do something like this in package.json to bundle everything except vscode:

{
  "targets": {
    "default": {
      "includeNodeModules": { "vscode": false }
    }
  }
  "engines": {
    "node": ">= 12",
  }
}

https://v2.parceljs.org/configuration/package-json/#includenodemodules

@sketchbuch
Copy link

sketchbuch commented Dec 3, 2020

Hi @mischnic
I tried to use your config but it is not working.

https://github.com/sketchbuch/vsc-packages/tree/feature/parcel2

If I run the build command, it builds but doesn't include the dependencies. Is my package.json correct? My targets object seems to be ignored

@sketchbuch
Copy link

sketchbuch commented Dec 3, 2020

Ignore my last comment, In addition to parcel 2 I also still had parcel 1 in my package.json and that was being used it seems. also needed to upgrade node to 12.18.3 to fix #5245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants