2016-08-09 55 views
0

我为我的项目中的某个模块使用第三方库。我也想为图书馆做出贡献,所以我使用npm链接,以便我的代码更改更容易管理。当我试图通过NPM通常包括库/ node_modules包加载成功,但是当我移动到另一个文件夹,使用NPM链接它抛出这个错误节点模块工作,但不是如果使用npm链接

ERROR in ../react-google-maps/lib/utils/index.js 
Module not found: Error: Cannot resolve module 'babel' 

这里是我的package.json

{ 
    "name": "Test", 
    "version": "1.0.0", 
    "description": "Logistics", 
    "author": "wnoveno", 
    "main": "index.js", 
    "scripts": { 
    "start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js --port 9000", 
    "build": "webpack -p", 
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test", 
    "test:watch": "npm run test -- --watch" 
    }, 
    "license": "ISC", 
    "devDependencies": { 
    "babel-cli": "^6.5.1", 
    "babel-core": "^6.5.2", 
    "babel-loader": "^6.2.0", 
    "babel-preset-es2015": "^6.1.18", 
    "babel-preset-react": "^6.1.18", 
    "babel-preset-stage-0": "^6.5.0", 
    "babel-preset-stage-1": "^6.1.18", 
    "chai": "^3.5.0", 
    "chai-jquery": "^2.0.0", 
    "jquery": "^2.2.1", 
    "jsdom": "^8.1.0", 
    "mocha": "^2.4.5", 
    "webpack": "^1.12.9", 
    "webpack-dev-server": "^1.14.0" 
    }, 
    "dependencies": { 
    "axios": "^0.11.0", 
    "babel-runtime": "^6.9.2", 
    "classnames": "^2.2.5", 
    "css-loader": "^0.23.1", 
    "extract-text-webpack-plugin": "^1.0.1", 
    "http-server": "^0.9.0", 
    "immutable": "^3.8.1", 
    "lodash": "^4.13.1", 
    "moment": "^2.13.0", 
    "node-sass": "^3.4.2", 
    "postcss-loader": "^0.9.1", 
    "react": "^15.0.0", 
    "react-big-calendar": "^0.9.12", 
    "react-bootstrap": "^0.29.5", 
    "react-cookie": "^0.4.6", 
    "react-dom": "^15.0.0", 
    "react-redux": "^4.4.5", 
    "react-redux-loading-bar": "^2.0.2", 
    "react-router": "^2.4.1", 
    "react-router-redux": "^4.0.5", 
    "react-tap-event-plugin": "^1.0.0", 
    "redux": "^3.0.4", 
    "redux-form": "^5.0.0", 
    "redux-localstorage": "^0.4.0", 
    "redux-persist": "^3.1.1", 
    "redux-promise": "^0.5.3", 
    "redux-promise-middleware": "^3.2.0", 
    "redux-thunk": "^2.1.0", 
    "sass-loader": "^3.2.0", 
    "style-loader": "^0.13.1" 
    } 
} 

和webpack.config

const path = require('path') 

module.exports = { 
    entry: [ 
    './src/index.js' 
    ], 
    output: { 
    path: __dirname, 
    publicPath: '/', 
    filename: 'bundle.js' 
    }, 
    module: { 
    loaders: [{ 
     exclude: /node_modules/, 
     loader: 'babel' 
    }, 
     { 
     test: /(\.scss|\.css)$/, 
     loaders: ['style', 'css', 'sass'] 
     }] 
    }, 
    resolve: { 
    root: [path.resolve(__dirname, 'src')], 
    extensions: ['', '.js', '.jsx', '.scss'], 
    modulesDirectories: [ 
     'node_modules', 
     path.resolve(__dirname, './node_modules'), 
     path.resolve(__dirname, './node_modules/react-google-maps/lib'), 

    ] 
    }, 

    // resolveLoader: { 
    //  modulesDirectories: [ 
    //   './node_modules/react-google-maps/node_modules/babel-cli' 
    //  ] 
    // }, 
    devServer: { 
    historyApiFallback: true, 
    contentBase: './' 
    }, 
    externals: { 
    'config': JSON.stringify(require('./config-dev.json')) 
    }, 

} 
const path = require('path') 

module.exports = { 
    entry: [ 
    './src/index.js' 
    ], 
    output: { 
    path: __dirname, 
    publicPath: '/', 
    filename: 'bundle.js' 
    }, 
    module: { 
    loaders: [{ 
     exclude: /node_modules/, 
     loader: 'babel' 
    }, 
     { 
     test: /(\.scss|\.css)$/, 
     loaders: ['style', 'css', 'sass'] 
     }] 
    }, 
    resolve: { 
    root: [path.resolve(__dirname, 'src')], 
    extensions: ['', '.js', '.jsx', '.scss'], 
    modulesDirectories: [ 
     'node_modules', 
     path.resolve(__dirname, './node_modules'), 
     path.resolve(__dirname, './node_modules/react-google-maps/lib'), 

    ] 
    }, 

    // resolveLoader: { 
    //  modulesDirectories: [ 
    //   './node_modules/react-google-maps/node_modules/babel-cli' 
    //  ] 
    // }, 
    devServer: { 
    historyApiFallback: true, 
    contentBase: './' 
    }, 
    externals: { 
    'config': JSON.stringify(require('./config-dev.json')) 
    }, 

} 

这里的包的的package.json

{ 
    "name": "react-google-maps", 
    "version": "5.0.0", 
    "description": "React.js Google Maps integration component", 
    "main": "src/index.js", 
    "files": [ 
    "lib/", 
    "src/", 
    "CHANGELOG.md" 
    ], 
    "scripts": { 
    "clean": "rimraf lib", 
    "prebuild": "npm run lint && npm run clean", 
    "build:watch": "npm run build -- --watch", 
    "build": "cross-env NODE_ENV=production babel src --out-dir lib", 
    "lint": "cross-env NODE_ENV=test eslint .", 
    "pretest:cov": "npm run lint", 
    "pretest": "npm run lint", 
    "test:cov": "cross-env NODE_ENV=test babel-node ./node_modules/.bin/isparta cover --report lcov _mocha -- $npm_package_config_mocha", 
    "test:watch": "npm test -- --watch", 
    "test": "cross-env NODE_ENV=test mocha $npm_package_config_mocha", 
    "prerelease": "npm run build && git add -A && git commit -m 'chore(lib): compile from src using babel'", 
    "release": "standard-version" 
    }, 
    "config": { 
    "mocha": "--compilers js:babel-register ./src/**/__tests__/*.spec.js --require ./src/__tests__/setup.js" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "https://github.com/tomchentw/react-google-maps" 
    }, 
    "keywords": [ 
    "React.js", 
    "React", 
    "react-component", 
    "google", 
    "map", 
    "maps", 
    "place", 
    "places", 
    "googlemap", 
    "googlemaps", 
    "google-map", 
    "google-maps", 
    "google map", 
    "google maps", 
    "GoogleMapsMixin", 
    "Map", 
    "Marker", 
    "Polyline", 
    "Polygon", 
    "Circle", 
    "Directions", 
    "InfoWindow", 
    "SearchBox" 
    ], 
    "author": { 
    "name": "tomchentw", 
    "email": "[email protected]", 
    "url": "https://github.com/tomchentw" 
    }, 
    "license": "MIT", 
    "bugs": { 
    "url": "https://github.com/tomchentw/react-google-maps/issues" 
    }, 
    "homepage": "https://tomchentw.github.io/react-google-maps/", 
    "devDependencies": { 
    "babel-cli": "^6.5.1", 
    "babel-core": "^6.5.2", 
    "babel-eslint": "^6.0.4", 
    "babel-plugin-lodash": "^3.1.4", 
    "babel-plugin-transform-flow-comments": "^6.5.0", 
    "babel-plugin-transform-runtime": "^6.9.0", 
    "babel-plugin-typecheck": "^3.6.1", 
    "babel-preset-es2015": "^6.5.0", 
    "babel-preset-react": "^6.5.0", 
    "babel-preset-stage-0": "^6.5.0", 
    "babel-register": "^6.5.2", 
    "codeclimate-test-reporter": "^0.3.1", 
    "cross-env": "^1.0.7", 
    "eslint": "^2.2.0", 
    "eslint-config-airbnb": "^9.0.1", 
    "eslint-plugin-flowtype": "^2.4.0", 
    "eslint-plugin-import": "^1.8.1", 
    "eslint-plugin-jsx-a11y": "^1.2.2", 
    "eslint-plugin-react": "^5.1.1", 
    "expect": "^1.14.0", 
    "isparta": "^4.0.0", 
    "istanbul": "^0.4.2", 
    "jsdom": "^9.2.1", 
    "mocha": "^2.4.5", 
    "react": "^15.1.0", 
    "react-dom": "^15.1.0", 
    "rimraf": "^2.5.2", 
    "standard-version": "^2.2.1", 
    "tomchentw-npm-dev": "^4.0.0" 
    }, 
    "dependencies": { 
    "babel-runtime": "^6.9.2", 
    "can-use-dom": "^0.1.0", 
    "google-maps-infobox": "^1.1.13", 
    "invariant": "^2.1.1", 
    "lodash": "^4.13.1", 
    "marker-clusterer-plus": "^2.1.4", 
    "react-prop-types-element-of-type": "^2.1.0", 
    "scriptjs": "^2.5.8", 
    "warning": "^2.1.0" 
    }, 
    "peerDependencies": { 
    "react": "^0.14.0 || ^15.0.0", 
    "react-dom": "^0.14.0 || ^15.0.0" 
    } 
} 
+0

你可以尝试在另一个文件夹中单独安装吗? – codeinprogress

+0

是在单独的文件夹上安装npm可以。但是,如果它在一个单独的文件夹中,webpack似乎无法识别加载器。这是我为什么不认可巴贝尔的猜测。 – wnoveno

+0

你能告诉我们*你究竟如何链接? –

回答

0

resolve配置不正确,因为的WebPack 1的resolve配置是混乱...

modulesDirectories不接受绝对路径(即您正在使用path.resolve构建的内容),但仅限于目录名称。默认modulesDirectories将做正确的事情。

你需要的是一个fallback指向path.resolve(__dirname, 'node_modules')(只会path.resolve(__dirname)工作?我不知道)。

npm link编辑模块本身需要另一个模块,由当时它试图resolve的依赖,这是你的项目的文件夹外,所以它并没有通过节点的正常决心访问您的node_modules(什么modulesDirectories器具) 。然后webpack的解析器会尝试查看fallback绝对路径。

相关问题