简体   繁体   English

我的 Vue NPM 导入只能在 src 文件夹中而不是项目根目录中找到模块

[英]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.

相关问题 Npm 链接尝试在 src 文件夹而不是 dist 文件夹中查找模块 - Npm link try to find module in src folder instead of dist folder 在我的 webpack npm 主项目内的子项目中使用 config 和 node_modules - Use config and node_modules, in my sub-projects inside of my webpack npm main project 如何在我的npm模块中写入另一个nodejs项目根文件夹 - How to write in another nodejs project root folder from my npm module 将CI用于Bitbucket存储库-我是否需要在根文件夹中包含node_modules? - Using CI for Bitbucket repository - do I need to have node_modules inside my root folder? npm文件夹内的嵌套节点模块文件夹 - Nested node-modules folder inside npm folder Neutrinojs / vue项目:从根文件夹导入vue文件/模板 - Neutrinojs/vue project: Importing vue files/templates from root folder npm没有在项目目录中创建node_modules文件夹 - npm not creating node_modules folder in project directory npm 安装不工作,从 git 克隆我的项目后无法安装任何节点模块 - npm install not working, can't install any of the node modules after cloning my project from git 我可以将 npm node_modules 目录放在我的项目之外吗 - Can I put the npm node_modules directory outside of my project 节点npm为什么将所有模块都安装在根node_modules文件夹中? - Node npm why all the modules are installed in the root node_modules folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM