为什么 require('webpack') 就会出错。


我的 webpack.config.js 是这样的:


 js


 var webpack = require('webpack');

module.exports = {
    entry: {
        zyfaq: "./app/zyfaq/zyfaq.js"
    },
    output: {
        path: __dirname + '/public/js',
        filename: "[name].min.js"
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: "style!css" }
        ]
    }
};

错误提示:


 module.js:338
    throw err;
          ^
Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/cos800/zhaoyang/webpack.config.js:2:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)

但是我把第一行 var webpack = require('webpack'); 去掉就不会出错了。

我又重新安装 webpack 并安装到项目内,又出现以下错误:


 % sudo cnpm install webpack
Password:
-
> [email protected] install /Users/cos800/zhaoyang/node_modules/webpack/node_modules/watchpack/node_modules/chokidar/node_modules/fsevents
> node-gyp rebuild

gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: getaddrinfo ENOTFOUND npm.taobao.org
gyp ERR! stack     at errnoException (dns.js:44:10)
gyp ERR! stack     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
gyp ERR! System Darwin 14.1.0
gyp ERR! command "node" "/usr/local/lib/node_modules/cnpm/node_modules/npm-for-cnpm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/cos800/zhaoyang/node_modules/webpack/node_modules/watchpack/node_modules/chokidar/node_modules/fsevents
gyp ERR! node -v v0.12.0
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok
npm WARN optional dep failed, continuing [email protected]
[email protected] node_modules/webpack
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])

node.js web前端开发 前端 webpack JavaScript

Callll酱 10 years, 1 month ago

npm install -g webpack
这样安装试一下?

最爱长门有希 answered 10 years, 1 month ago

npm 没有 install webpack ?

执行


 bash


 npm install webpack

crywolf answered 10 years, 1 month ago

看看你的 project.json 里有没有 "webpack": "^1.6.0"
如果没有就安装。


 npm install webpack --save

加到你的依赖里。
在你代码看来,你不需要引入的。一般是用到webpack的插件的时候才引用他。

死神栗子丶 answered 10 years, 1 month ago

Your Answer