2

Using @​now/node-server, I'm trying to achieve this:

const myLocalLibrary = require('@src/lib/myLocalLibrary');

Instead of

const myLocalLibrary = require('../../../lib/myLocalLibrary');

The problem is that I have tried multiple things that won't work, including:

Setting NODE_PATH=src and using require('src/...

Does not work because setting NODE_PATH as env has no effect

Patching require using module-alias (https://www.npmjs.com/package/module-alias)

Works locally, fails on Zeit because node can't find any files using the module.

I used:

require("module-alias").addAlias("~", __dirname);

Is there any way of achieving this?

Wes Souza
  • 601
  • 2
  • 8
  • 17
  • 1
    seems it is not possible according to the official Github repo https://github.com/zeit/next.js/issues/342 – iurii Dec 16 '18 at 19:45

2 Answers2

0

You can use this

    "build": {
     "env": {
        "NODE_PATH": "src/"
        }
      }
0

The reason your solution fails to build on ZEIT Now might be that it only works runtime, and Now needs to resolve the paths at build time. You can try using babel-plugin-root-import like described here instead.

Or if you just want to use absolute imports with Next.js and ZEIT Now, see this question.

ArneHugo
  • 6,051
  • 1
  • 26
  • 47