Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import external module (cjs + server.ts) #9

Closed
fchevallieratecna opened this issue Nov 7, 2023 · 2 comments
Closed

Cannot import external module (cjs + server.ts) #9

fchevallieratecna opened this issue Nov 7, 2023 · 2 comments

Comments

@fchevallieratecna
Copy link

fchevallieratecna commented Nov 7, 2023

Hi,

Thank you for your marvelous examples here :)

I just have a quick question concerning the import

I have a reproductible example here : https://github.com/fchevallieratecna/remix-v2-server

you can

git clone https://github.com/fchevallieratecna/remix-v2-server
cd remix-v2-server
npm i
npm run build
npm start

and you'll see

❯ npm start

> start
> cross-env NODE_ENV=production node server.js

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module 'server/middlewares/test'
Require stack:
- /private/tmp/remix-v2-server/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/private/tmp/remix-v2-server/server.js:31:19)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/private/tmp/remix-v2-server/server.js' ]
}

Do you have an idea on how I can import *.ts file right from express server.ts files ?

@xHomu
Copy link
Owner

xHomu commented Nov 7, 2023

This is because the tsconfig that the template comes with has a noEmit flag. Remix takes care of building everything in remix build -- everything excepts what's happening in your express server file. Adding the middleware to tsconfig won't do anything because of noEmit.

That part is a bit confusing, but this is why there's a seperate script in package.json specifically for server.ts in the build step "build:server": "esbuild --platform=node --format=cjs ./server.ts --outdir=./",

There are a couple ways to fix this:

  1. Keep using esbuild & update the script: build:server": "esbuild --platform=node --format=cjs ./server.ts ./server/middlewares/test.ts --outdir=./",
  2. Don't use esbuild and create a new tsconfig profile for the server.

Option 2 is what we use on mana.wiki as well, since the express server itself is light enough that esbuild performance doesn't make a huge difference https://github.com/manawiki/core/blob/main/package.json#L8

I posted a PR to show how it works: fchevallieratecna/remix-v2-server#1

Note that when deploying, you have to make sure the built files are actually shipped. You may want to change the outDir to somewhere else to simplify the process.

@xHomu xHomu closed this as completed Nov 7, 2023
@fchevallieratecna
Copy link
Author

Wow that's much more than what I expected. I hope it will help some other people if needed :)

Many thanks to you !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants