【问题标题】:npm start failed to compilenpm start 编译失败
【发布时间】:2019-05-26 04:32:44
【问题描述】:

这是 npm start 的问题,运行 react-scripts start。请问,谁能帮帮我?

我正在尝试在我的应用程序中使用 Swiper,但我在控制台上收到此错误。

!UPDATED:添加了使用 swiper 的代码。

Failed to compile.
./node_modules/swiper/dist/js/swiper.esm.bundle.js
Module not found: Can't resolve '/home/lemes/Projects/idealnemoloko-react/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/assertThisInitialized' in '/home/lemes/Projects/idealnemoloko-react/node_modules/swiper/dist/js'

Laso 为您提供 package.json

我希望这会有所帮助

package.json

{
  "name": "idealnemoloko-react",
  "version": "0.1.0",
  "private": true,
  "homepage": ".",
  "dependencies": {
    "gh-pages": "^2.0.1",
    "gsap": "^2.0.2",
    "react": "^16.6.0",
    "react-art": "^16.6.1",
    "react-dom": "^16.6.0",
    "react-native": "^0.57.4",
    "react-native-web": "^0.9.6",
    "react-on-images-loaded": "^2.1.9",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.1",
    "swiper": "^4.4.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

我使用 swiper 的文件: 食谱/index.js

this.swiper = new Swiper('.recipes-swiper-container', {
  direction: 'vertical',
  speed: 1000,
  slidesPerView: 1,
  allowTouchMove: false,
  mousewheel: true,
  pagination: {
    el: '.recipes-swiper-pagination',
    clickable: true,
  },
  on: {
    transitionEnd: this.handleSlideTrans
  }
});

main/index.js

 hasLoaded() {
    if(this.state.isLoading === true) {
      this.setState({isLoading: false})

      this.swiper = new Swiper('.swiper-container', {
        direction: 'vertical',
        preventInteractionOnTransition: true,
        speed: 1000,
        autoplay: {
          delay: 5000,
          disableOnInteraction: false,
        },
        slidesPerView: 1,
        initialSlide: this.props.index,
        allowTouchMove: false,
        mousewheel: {
          eventsTarged: '#main-page'
        },
        pagination: {
          el: '.swiper-pagination',
          type: 'bullets',
          clickable: true
        },
      });

      this.swiperInfo = new Swiper('.swiper-container-info', {
        direction: 'vertical',
        preventInteractionOnTransition: true,
        speed: 1000,
        slidesPerView: 1,
        allowTouchMove: false,
        initialSlide: this.props.index
      });

      this.swiper.on('slideNextTransitionStart', () => {
        this.swiperInfo.slideNext();
      });
      this.swiper.on('slidePrevTransitionStart', () => {
        this.swiperInfo.slidePrev();
      });

      var pag = document.getElementsByClassName('swiper-pagination')[0].clientHeight;
      document.getElementById('info-back').style.top = 'calc(50% - ' + pag/window.innerHeight*100/3.25 + 'vh)';
    }
  }

  swiperStart() {
    this.swiper.autoplay.start();
  }

为什么/index.js

this.swiper = new Swiper('.why-swiper-container', {
      direction: 'vertical',
      speed: 1000,
      slidesPerView: 1,
      allowTouchMove: false,
      mousewheel: true,
      pagination: {
        el: '.why-swiper-pagination',
        clickable: true,
      },
      on: {
        transitionEnd: this.handleSlideTrans
      }
    });

【问题讨论】:

  • 您可以在使用 swiper 代码的地方添加组件代码吗?

标签: reactjs npm package.json npm-start


【解决方案1】:

我认为您需要Swiper

您需要像这样导入Swiper 包:

import Swiper from 'swiper/dist/js/swiper.js'

【讨论】:

  • 所以如果我使用import Swiper from 'swiper'; 这还不够,我需要从'swiper/dist/js/swiper.js' 导入它?
  • 是的,如果从导出路径导入,则需要导入。参考:github.com/nolimits4web/Swiper/issues/2335
  • 谢谢 问题好像解决了,但是又出现了一个:./src/NotFound.js Module not found: Can't resolve '/home/lemes/Projects/idealnemoloko-react/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/classCallCheck' in '/home/lemes/Projects/idealnemoloko-react/src' 但是文件直接在src目录下,我在App.js中导入是这样的:import NotFound from './NotFound';
  • on 来自 babel。您需要检查版本并导入
  • 我刚刚删除了 node_modules 并运行了 npm install。从现在开始一切正常。非常感谢!
猜你喜欢
  • 2021-12-22
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多