Error: Couldn't find preset “react” when installed using npm install --global babel-preset-react but works without global flag

后端 未结 2 1310
逝去的感伤
逝去的感伤 2021-01-01 18:25

I installed Babel CLI (version 6) using npm install --global babel-cli. I then install react preset using npm install --global babel-preset-react.

相关标签:
2条回答
  • 2021-01-01 19:09

    You can specify the absolute (or relative) path to the preset you are trying to use, e.g:

    babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/
    
    0 讨论(0)
  • 2021-01-01 19:16

    Optional Fix

    You can do it this way. Write these lines in your prompt. Now the only thing is with global you might have to use the fix suggested above by @Petar which is

    babel --presets /usr/local/lib/node_modules/babel-preset-react --watch jsx/ --out-dir js/
    

    but this one does all you need.

    npm i babel-cli babel-preset-react 
    
    babel --presets react jsx/ --watch --out-dir js/
    

    and Then optionally add a .gitignore file in your github repo with content = node_modules/ now run your jsx transformation with the same command.

    0 讨论(0)
提交回复
热议问题