Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add NODE_PATH to webpack in package.json?

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?

like image 745
Run Avatar asked Jun 04 '26 13:06

Run


1 Answers

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

like image 178
Patrick Hund Avatar answered Jun 07 '26 23:06

Patrick Hund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!