簡體   English   中英

BABEL 找不到模塊“節點:路徑”錯誤 react-native

[英]BABEL Cannot find module 'node:path' error react-native

我正在嘗試將 tailwindcss -react-native package 設置到我的 react native 項目中。 成功安裝后,當我將類名添加到組件時,它會給我一個錯誤。 我試過卸載然后重新安裝它,刪除 npm 緩存和 node_modules 文件夾,但我不知道出了什么問題。 我附上了錯誤日志以及我的項目的代碼。 請指出我在這里缺少的東西。 謝謝

錯誤日志截圖

我的 babel.config.js 文件:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: ["tailwindcss-react-native/babel"],
  };
};

我的 tailwind.config.js 文件:

module.exports = {
  content: [
    "./screens/**/*.{js,ts,jsx,tsx}",
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

我的 App.js 文件:

import { TailwindProvider } from 'tailwindcss-react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';

const Stack = createNativeStackNavigator();

export default function App() {
  return (
    <NavigationContainer>
      <TailwindProvider>
        <Stack.Navigator>
          <Stack.Screen name="Home" component={HomeScreen} />
        </Stack.Navigator>
      </TailwindProvider>
    </NavigationContainer>
    
  );
}

我的 HomeScreen.js 屏幕:

import { View, Text } from 'react-native'
import React from 'react'

export default function HomeScreen() {
  return (
    <View>
      <Text className="text-red-500">HomeScreen</Text>
    </View>
  )
}

我的依賴項和開發依賴項:

"dependencies": {
    "@react-navigation/native": "^6.0.11",
    "@react-navigation/native-stack": "^6.7.0",
    "expo": "~45.0.0",
    "expo-status-bar": "~1.3.0",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-native": "0.68.2",
    "react-native-safe-area-context": "4.2.4",
    "react-native-screens": "~3.11.1",
    "react-native-web": "0.17.7",
    "tailwindcss-react-native": "^1.7.10"
  },
  "devDependencies": {
    "@babel/cli": "^7.18.6",
    "@babel/core": "^7.18.6",
    "@babel/node": "^7.18.6",
    "tailwindcss": "^3.1.4"
  },

我解決我的方法是重新安裝我的應用程序
然后在安裝之后,我刪除了 package-lock.json 和 node_modules 並重新安裝了 tailwind-css-react-native 並在文檔中進行了設置並且它起作用了。

對於遇到此錯誤的每個人,解決方案是至少在14.18.0上更新Node
tailwindcss-react-native包正在嘗試訪問node:path變量,該變量可從版本 14.18.0 `

問題是你的節點版本......你需要做的就是將你的節點更新為最新版本,刪除 package.json node_modules 文件夾,然后 npm install

修復此錯誤所需要做的就是

  1. 安裝最新版本的 Node js。

  2. 安裝這個 Babel 插件 yarn add @babel/plugin-transform-react-jsx --dev

  3. 將此代碼添加到您的 babel 配置 js "@babel/plugin-transform-react-jsx"

只需按照此處的指南安裝即可

npm install nativewind
npm install --save-dev tailwindcss

https://www.nativewind.dev/guides/babel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM