如何在 package.json 中将 NODE_PATH 添加到 webpack? [英] How to add NODE_PATH to webpack in package.json?

查看:79
本文介绍了如何在 package.json 中将 NODE_PATH 添加到 webpack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I add NODE_PATH to webpack in package.json?

我的 packcage.json 的一部分:

Part of my 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:

backpack.config.js:

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

在我的 server.js 中:

In my 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,它工作正常.

But if I run npm start, it is working fine.

有什么想法吗?

推荐答案

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

You should edit your webpack configuration to include the app directory for resolving modules.

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

Based on your code example, it would look like this:

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

详情请参阅 webpack 文档:

See webpack documentation for details:

https://webpack.js.org/configuration/resolve/#resolve-模块

这篇关于如何在 package.json 中将 NODE_PATH 添加到 webpack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆