如何将NODE_PATH添加到package.json中的webpack?

时间:2017-08-26 09:41:57

标签: node.js webpack package.json

如何在package.json中将NODE_PATH添加到webpack?

我的packcage.json的一部分:

  "dependencies": {
    "axios": "^0.16.2",
    "cross-env": "^5.0.1",
    "koa": "^2.3.0",
    "koa-mount": "^3.0.0",
    "koa-static": "^4.0.1",
    "koa-trie-router": "^2.1.5",
    "mongodb": "^2.2.31",
    "nuxt": "^1.0.0-rc3",
    "socket.io": "^2.0.3"
  },
  "devDependencies": {
    "babel-eslint": "^7.2.3",
    "backpack-core": "^0.4.1"
  },
  "scripts": {
    "test": "mocha --harmony",
    "dev": "NODE_PATH=./app backpack dev",
    "build": "nuxt build && backpack build",
    "start": "cross-env NODE_ENV=production NODE_PATH=./app node build/main.js"
  },

backpack.config.js:

module.exports = {
  webpack: (config, options, webpack) => {
    config.entry.main = './server/index.js'
    return config
  }
}

在我的server.js中:

import Koa from 'koa'
import { Nuxt, Builder } from 'nuxt'
import socket from 'socket.io'
import http from 'http'
import config from 'config' // this is './config' without NODE_PATH=./app
npm run dev处的

错误

This dependency was not found:

* config in ./server/index.js

但如果我运行npm start,它运行正常。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您应该编辑您的webpack配置,以包含用于解析模块的 app 目录。

根据您的代码示例,它看起来像这样:

module.exports = {
  webpack: (config, options, webpack) => {
    config.entry.main = './server/index.js'
    config.resolve.modules = ['./app']
    return config
  }
}

有关详细信息,请参阅webpack文档:

https://webpack.js.org/configuration/resolve/#resolve-modules