V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
snowrain
V2EX  ›  前端开发

Webpack 如何 require 不在项目目录下的模块?

  •  1
     
  •   snowrain · 2016-04-26 11:39:38 +08:00 · 6245 次点击
    这是一个创建于 2939 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目录结构

    - Common
     - a.js
     - b.js
     
    - Project
     - webpack.config.js
     - src
      - module.js
      - main.js
    

    webpack.config.js 里已有配置:

    babel: {
       presets: ['es2015'],
       plugins: ['transform-runtime']
    },
    resolve: {
       extensions: ['', '.js'],
       root: '/common'
    }
    

    main.js 里面 require Common 目录下的模块

    require('a.js');
    

    运行时会报错:

    ERROR in /Common/a.js
    Module build failed: ReferenceError: Unknown plugin "transform-runtime" specifie
    d in "base" at 0, attempted to resolve relative to "/Common/a.js"
    

    如果在 main.js 里面 require Project 下的模块就正常

    require('./module.js');
    

    那么问题来了,如何正常引入 Common 文件夹内的模块?

    2 条回复    2016-04-26 11:53:39 +08:00
    jsonline
        1
    jsonline  
       2016-04-26 11:45:26 +08:00 via Android
    JS 也到了问配置问题的时代了吗?

    我看到了 Java 的影子
    snowrain
        2
    snowrain  
    OP
       2016-04-26 11:53:39 +08:00
    @jsonline 是工具就会有配置问题,与语言无关吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1446 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:18 · PVG 01:18 · LAX 10:18 · JFK 13:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.