[英]My Vue NPM imports can only find the modules if they are inside the src folder and not project root
In my app root, I have /node_modules/ and /src/在我的应用程序根目录中,我有 /node_modules/ 和 /src/
I have ran npm install and installed packages using npm install axios --save etc for all my dependencies I do see that they are being added to the node_modules directory however my vue project does not compile with the following error:我已经运行 npm install 并使用 npm install axios --save 等安装了我的所有依赖项我确实看到它们被添加到 node_modules 目录但我的项目没有以下错误:
Failed to compile.
./src/main.js
Module not found: Error: Can't resolve 'axios' in '/app/src'
If I manually create a node_modules folder inside of my /src/ directory, and place axios, and my other dependencies it works.. but from what I've read I should not be handling my node modules this way.如果我在我的 /src/ 目录中手动创建一个 node_modules 文件夹,并放置 axios 和我的其他依赖项,它可以工作.. 但从我读过的内容来看,我不应该以这种方式处理我的节点模块。 It seems I need to have my dependencies point to a different path?
看来我需要让我的依赖项指向不同的路径?
What's weird is my import Vue from 'vue' and App from './App.vue' work fine, it's basically everything afterwards that fails to compile..奇怪的是我从 'vue' 导入 Vue 和从 './App.vue' 导入 App 工作正常,基本上是之后的所有内容都无法编译。
My main.js:我的 main.js:
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import firebase from 'firebase'
import "firebase/auth";
import "firebase/analytics";
import "firebase/firestore";
import moment from 'moment'
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
new Vue({
render: h => h(App),
firebase: firebase,
}).$mount('#app')
I have also tried removing all node modules, clearing NPM cache and reinstalling NPM dependencies to no avail.我还尝试删除所有节点模块,清除 NPM 缓存并重新安装 NPM 依赖项,但无济于事。
instead of use the use
function to add your axios instance globally can you please try to add it like this:而不是
use
function 全局添加您的 axios 实例,您可以尝试像这样添加它:
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import firebase from 'firebase'
import "firebase/auth";
import "firebase/analytics";
import "firebase/firestore";
import moment from 'moment'
Vue.config.productionTip = false
Vue.prototype.$axios = Axios; //<---- like that
new Vue({
render: h => h(App),
firebase: firebase,
}).$mount('#app')
after that try to compile again.之后尝试再次编译。 you are then able to call your axios instance on every component with
this.$axios
然后,您可以使用
this.$axios
在每个组件上调用您的 axios 实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.