React Native'无法解析模块`moment-timezone`',尽管它在node_modules中

时间:2018-03-26 22:24:12

标签: javascript react-native yarnpkg

我正在尝试运行一个React Native项目lucy-app,它通过Yarn管理它的包。在src目录中有一个utils.js

Kurts-MacBook-Pro-2:lucy kurtpeek$ tree lucy-app/src -L 1
lucy-app/src
├── actions.js
├── api.js
├── app.js
├── components
├── containers
└── utils.js

momentmoment-timezone都显示在package.json

{
    "name": "app",
    "version": "0.0.1",
    "private": true,
    "scripts": {
    },
    "dependencies": {
        "moment": "^2.19.0",
        "moment-timezone": "^0.5.14",

moment-timezone中有一个node_modules目录:

/Users/kurtpeek/Documents/dev/lucy/lucy-app/node_modules
Kurts-MacBook-Pro-2:node_modules kurtpeek$ find . -name 'moment-timezone'
./moment-timezone

utils.js文件导入moment-timezone,如下所示:

import moment from 'moment-timezone';

但是,当我尝试运行react-native run-ios时,出现以下错误:

error: bundling failed: Error: Unable to resolve module `moment-timezone` from `/Users/kurtpeek/Documents/Dev/lucy/lucy-app/src/utils.js`: Module does not exist in the module map or in these directories:
  /Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules

This might be related to https://github.com/facebook/react-native/issues/4968
To resolve try the following:
  1. Clear watchman watches: `watchman watch-del-all`.
  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
  3. Reset Metro Bundler cache: `rm -rf $TMPDIR/react-*` or `npm start -- --reset-cache`.  4. Remove haste cache: `rm -rf $TMPDIR/haste-map-react-native-packager-*`.
    at ModuleResolver.resolveDependency (/Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:311:1719)
    at ResolutionRequest.resolveDependency (/Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:96:16)
    at DependencyGraph.resolveDependency (/Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/node-haste/DependencyGraph.js:269:4352)
    at /Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/DeltaBundler/traverseDependencies.js:201:36
    at Generator.next (<anonymous>)
    at step (/Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/DeltaBundler/traverseDependencies.js:256:306)
    at /Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/DeltaBundler/traverseDependencies.js:256:536
    at new Promise (<anonymous>)
    at /Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/DeltaBundler/traverseDependencies.js:256:217
    at addDependency (/Users/kurtpeek/Documents/Dev/lucy/lucy-app/node_modules/metro/src/DeltaBundler/traverseDependencies.js:256:92)
 BUNDLE  [ios, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.

根据建议,我已尝试rm -rf node_modules后跟yarn install,但我仍然遇到同样的错误。任何想法在这里出了什么问题?

0 个答案:

没有答案