How to add NODE_PATH to webpack in package.json?

社会主义新天地 提交于 2021-02-09 11:54:11

问题


How do I add NODE_PATH to webpack in package.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:

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

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

Error at npm run dev:

This dependency was not found:

* config in ./server/index.js

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

Any ideas?


回答1:


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
  }
}

See webpack documentation for details:

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



来源:https://stackoverflow.com/questions/45894047/how-to-add-node-path-to-webpack-in-package-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!