admin管理员组文章数量:1201837
I created a task in Visual Studio Code to fetch some data from a web API and put it in the project files. For the fetch task, I tried to use the "axios" node module, which I installed globally:
npm install -g axios
npm list -g axios
[email protected]
However, when I try to run the task, it fails with an error that 'axios' can't be found.
Error: Cannot find module 'axios'
This is the javascript file:
const axios = require('axios');
async function downloadFile() {
var url = "some-url"
const response = await axios.get(url);
console.log(response.data);
}
async function main() {
await downloadFile();
}
main();
And this is the tasks definition JSON file:
{
"version": "2.0.0",
"tasks": [
{
"label": "Fetch Project Data Files",
"type": "shell",
"command": "node",
"args": [
"${workspaceFolder}/.vscode/fetch-data-files.js"
],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
What am I doing wrong here? Why won't the task runner recognize global node modules? I know that I can use http instead of axios, install axios locally, or some other workarounds, but I'm trying to understand the underlying problem.
I created a task in Visual Studio Code to fetch some data from a web API and put it in the project files. For the fetch task, I tried to use the "axios" node module, which I installed globally:
npm install -g axios
npm list -g axios
[email protected]
However, when I try to run the task, it fails with an error that 'axios' can't be found.
Error: Cannot find module 'axios'
This is the javascript file:
const axios = require('axios');
async function downloadFile() {
var url = "some-url"
const response = await axios.get(url);
console.log(response.data);
}
async function main() {
await downloadFile();
}
main();
And this is the tasks definition JSON file:
{
"version": "2.0.0",
"tasks": [
{
"label": "Fetch Project Data Files",
"type": "shell",
"command": "node",
"args": [
"${workspaceFolder}/.vscode/fetch-data-files.js"
],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
What am I doing wrong here? Why won't the task runner recognize global node modules? I know that I can use http instead of axios, install axios locally, or some other workarounds, but I'm trying to understand the underlying problem.
Share Improve this question edited Jan 22 at 8:21 Tomer B asked Jan 22 at 6:56 Tomer BTomer B 5,4658 gold badges27 silver badges49 bronze badges 3 |1 Answer
Reset to default 0I found the solution. Press CTRL+SHIFT+P to open the commands window, select Preferences: Open User Settings (JSON)
to open the JSON configuration file for VS code.
Then add the following lines (replace USERNAME with your own windows profile username):
"terminal.integrated.env.windows": {
"NODE_PATH": "C:\\Users\\USERNAME\\AppData\\Roaming\\npm\\node_modules"
}
This will set the NODE_PATH variable for the integrated terminal in VS Code which is used to run the tasks, and with the NODE_PATH set, it will know where to find the global installed packages.
Note: In most cases, this would not be the best practice. It would be better to install dependent modules inside the project that depends on them, to avoid version conflicts and to make sure it will work in different environments without problems.
本文标签: nodejsIn a VS Code taskhow can I use my global node modulesStack Overflow
版权声明:本文标题:node.js - In a VS Code task, how can I use my global node modules? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738582207a2101232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
npm i -g axios
?npm -v axios
doesn't make sense because -v shows npm version and ignores "axios" or whatever comes next – Estus Flask Commented Jan 22 at 8:02npm install -g axios
, I just got the command to show that it's installed wrong. Updated the question. – Tomer B Commented Jan 22 at 8:22