求助:使用vsce打包VSCode扩展时遭遇无效相对路径错误(npm 8.1.0、vsce 2.5.3)
The error you're seeing happens because npm uses symlinks when installing local file dependencies like "colibri": "file:../colibri". When vsce package runs, it tries to follow these symlinks, but since your colibri folder's node_modules are outside the vscode-teroshdl directory, it generates invalid relative paths that traverse beyond the extension's root. Here are two reliable solutions to fix this:
Solution 1: Use npm pack to Install from a Tarball
This method packages your colibri dependency into a tarball, which npm will copy directly into your extension's node_modules (no symlinks involved):
- Navigate to your
colibrifolder and install its dependencies:cd colibri npm install - Create a tarball of the
colibripackage:
This generates a file likenpm packcolibri-<your-version>.tgzin thecolibrifolder. - Go back to the
vscode-teroshdlfolder:cd ../vscode-teroshdl - Update your
package.jsonto reference the tarball instead of the file path:
Replace"dependencies": { "colibri": "../colibri/colibri-<your-version>.tgz" }<your-version>with the actual version number from the tarball filename. - Install the dependency from the tarball:
npm install - Now run
vsce package—the packaging should complete without the relative path error.
Solution 2: Install Local Dependency Without Symlinks
Use npm's --no-symlink flag to copy the entire colibri folder (including its node_modules) into your extension's directory:
- In the
colibrifolder, ensure dependencies are installed:cd colibri npm install - Navigate to
vscode-teroshdland installcolibriwithout symlinks:cd ../vscode-teroshdl npm install ../colibri --no-symlink - Run
vsce package—all files are now within the extension's directory, so the path error is resolved.
Note
If you make changes to colibri later, you'll need to repeat the respective steps to update the dependency in vscode-teroshdl before packaging again.
内容的提问来源于stack exchange,提问作者Carlos Alberto Ruiz Naranjo

