snowrain

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

  •  1
     
  •   snowrain · Apr 26, 2016 · 7490 views
    This topic created in 3848 days ago, the information mentioned may be changed or developed.

    目录结构

    - 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 replies  •  2016-04-26 11:53:39 +08:00
    jsonline
    jsonline
    Apr 26, 2016 via Android
    1
    JS 也到了问配置问题的时代了吗?

    我看到了 Java 的影子
    snowrain
    snowrain
    OP
    Apr 26, 2016
    2
    @jsonline 是工具就会有配置问题,与语言无关吧。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1558 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 157ms · UTC 12:14 · PVG 20:14 · LAX 04:14 · JFK 07:14
    ♥ Do have faith in what you're doing.