在 jsconfig.json 中使用 baseUrl 不适用于 NextJS [英] Using baseUrl in jsconfig.json not working with NextJS

查看:96
本文介绍了在 jsconfig.json 中使用 baseUrl 不适用于 NextJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://github.com/hutber/jsconfigerror 示例存储库显示 jsconfig 不起作用.

https://github.com/hutber/jsconfigerror Example repo showing the jsconfig not working.

我的 jsconig.json 文件中有以下设置:

I have the following set inside my jsconig.json file:

{
  "compilerOptions": {
    "baseUrl": "./"
  }
}

但是,当我进行导入时,它会失败:

However when I do an import it fails:

./pages/index.js
Module not found: Can't resolve 'components/AThing' in '/var/www/gd.hutuber.com/pages'

文件夹结构

¬ components
   ¬ AThing
¬ pages
   ¬ index.js

页面/index.js

import Head from 'components/AThing'

如何让 baseUrlnextjs

推荐答案

next 不读取 jsconfig.json 中写的配置,必须按照 nextjs 的方式自定义 webpack config.

Next doesn't read the configurations written in jsconfig.json, you have to customize webpack config the nextjs way.

在项目根目录下 package.json 旁边创建一个名为 next.config.js 的文件.然后重启.

Create a file named next.config.js at the root directory of your project next to package.json. Then restart.

const path = require('path')

module.exports = {
  webpack: config => {
    config.resolve.modules.push(path.resolve('./'))

    return config
  }
}


这篇关于在 jsconfig.json 中使用 baseUrl 不适用于 NextJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆