snowrain

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

  •  1
     
  •   snowrain · Apr 26, 2016 · 7123 views
    This topic created in 3678 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
        1
    jsonline  
       Apr 26, 2016 via Android
    JS 也到了问配置问题的时代了吗?

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