简体   繁体   English

React Util js 文件不能使用绝对路径?

[英]React Util js file not working with absolute path?

I'm using create-react-app.我正在使用 create-react-app。 No eject.没有弹出。

I need help figuring out why this doesn't work.我需要帮助弄清楚为什么这不起作用。

import { capitalizeFirst } from 'util/util.js'; //This one doesn't work

import AdminInput from 'components/AdminInput/AdminInput.jsx';

console.log(capitalizeFirst); // returns undefined

and my .env和我的 .env

NODE_PATH=src/

and my util.js和我的 util.js

export function capitalizeFirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

The other components work as expected.其他组件按预期工作。 Using the function will also result in "TypeError: _util.capitalizeFirst is not a function" or something like that.使用该函数也会导致“TypeError: _util.capitalizeFirst is not a function”或类似的结果。

However, if I use但是,如果我使用

import { capitalizeFirst } from '../../util/util.js';

it will print the correct function instead.它将打印正确的函数。

I don't understand why the js file doesn't work but the React Components work with an absolute path?我不明白为什么 js 文件不起作用,但 React 组件使用绝对路径?

Util 在源代码中...

它从node_modules解析为util/util

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM