Nuxt 2 Injecting Plugins & Typescript Dependencies - Error: relative module was not found: * ./index.vue?vue&type=script&lang=ts&
Nuxt 2 Injecting Plugins & Typescript Dependencies - Error: relative module was not found: * ./index.vue?vue&type=script&lang=ts&
nuxt 2.15.8
@nuxt/typescript-build 2.1.0
- “打字稿”:“~4.2”
我正在尝试遵循 Nuxt Typescript 插件的说明书 https://typescript.nuxtjs.org/cookbook/plugins/
在我的 Nuxt 项目中,我创建了一个自定义插件文件 /plugins/hello.ts
import { Plugin } from "@nuxt/types";
const helloPlugin: Plugin = (context, inject) => {
inject("hello", () => alert("hello"));
};
export default helloPlugin;
在nuxt.config.js
中:
export default {
plugins: ['~/plugins/hello']
但是我得到这个错误:
This dependency was not found:
* nuxt_plugin_hello_34b291c8 in ./.nuxt/index.js
和./.nuxt/index.js
import nuxt_plugin_plugin_8b6d7ab8 from 'nuxt_plugin_plugin_8b6d7ab8' // Source: .\vuetify\plugin.js (mode: 'all')
import nuxt_plugin_hello_785142de from 'nuxt_plugin_hello_785142de' // Source: ..\plugins\hello.ts (mode: 'client')
说明书没有提到在 nuxt.config.js
中注册插件,但我认为 Nuxt 2 隐含了该步骤。
配置可能有什么问题?我需要在某处指定文件的转译文件吗?
更新
我尝试添加一个基于 typescript 的 vue 文件并得到这个错误:
relative module was not found: * ./index.vue?vue&type=script&lang=ts& in ./pages/index.vue
所以打字稿工具有问题。
原来是 typescript
的依赖解析问题。
我的项目在 typescript 中没有 Vue 文件,api 和插件在 typescript 中,所以我一直在使用 typescript 工具,直到我尝试将它绑定到 Nuxt 工具中:
我使用 create-nuxt-app
工具创建了 Nuxt 项目,然后使用 npm
(7.23.0) 和 typescript failing to be resolved correctly with Nuxt tooling is a known issue with npm install
of the dependencies that create-nuxt-app
adds to the package.json.
安装了软件包
我的解决方案是使用 yarn
进行安装 ♂️(你不知道 yarn 在依赖树方面更好吗?哈哈)。
所以我用 yarn 重新安装了依赖项,然后 typescript 与 Nuxt 工具一起工作,问题的示例插件代码都按预期工作。
*我更新了问题以包括人们可能遇到的更常见的错误,这实际上更能说明问题是什么。
nuxt 2.15.8
@nuxt/typescript-build 2.1.0
- “打字稿”:“~4.2”
我正在尝试遵循 Nuxt Typescript 插件的说明书 https://typescript.nuxtjs.org/cookbook/plugins/
在我的 Nuxt 项目中,我创建了一个自定义插件文件 /plugins/hello.ts
import { Plugin } from "@nuxt/types";
const helloPlugin: Plugin = (context, inject) => {
inject("hello", () => alert("hello"));
};
export default helloPlugin;
在nuxt.config.js
中:
export default {
plugins: ['~/plugins/hello']
但是我得到这个错误:
This dependency was not found:
* nuxt_plugin_hello_34b291c8 in ./.nuxt/index.js
和./.nuxt/index.js
import nuxt_plugin_plugin_8b6d7ab8 from 'nuxt_plugin_plugin_8b6d7ab8' // Source: .\vuetify\plugin.js (mode: 'all')
import nuxt_plugin_hello_785142de from 'nuxt_plugin_hello_785142de' // Source: ..\plugins\hello.ts (mode: 'client')
说明书没有提到在 nuxt.config.js
中注册插件,但我认为 Nuxt 2 隐含了该步骤。
配置可能有什么问题?我需要在某处指定文件的转译文件吗?
更新
我尝试添加一个基于 typescript 的 vue 文件并得到这个错误:
relative module was not found: * ./index.vue?vue&type=script&lang=ts& in ./pages/index.vue
所以打字稿工具有问题。
原来是 typescript
的依赖解析问题。
我的项目在 typescript 中没有 Vue 文件,api 和插件在 typescript 中,所以我一直在使用 typescript 工具,直到我尝试将它绑定到 Nuxt 工具中:
我使用 create-nuxt-app
工具创建了 Nuxt 项目,然后使用 npm
(7.23.0) 和 typescript failing to be resolved correctly with Nuxt tooling is a known issue with npm install
of the dependencies that create-nuxt-app
adds to the package.json.
我的解决方案是使用 yarn
进行安装 ♂️(你不知道 yarn 在依赖树方面更好吗?哈哈)。
所以我用 yarn 重新安装了依赖项,然后 typescript 与 Nuxt 工具一起工作,问题的示例插件代码都按预期工作。
*我更新了问题以包括人们可能遇到的更常见的错误,这实际上更能说明问题是什么。