luanglopes / ts-paths-esm-loader

Node JS loader for Typescript that supports tsconfig paths
MIT License
23 stars 5 forks source link

New referenceerror: exports is not defined in ES module scope #10

Closed wvhulle closed 3 months ago

wvhulle commented 1 year ago

I have a script on node v18.12.1:

import * as pg from 'pg'
const { Client } = pg

async function main() {

    const client = new Client({

        connectionString: process.env.DATABASE_URL
    })
    await client.connect()

    const res = await client.query(`DELETE FROM "Regeneration" WHERE`)

    console.log({ res })
    await client.end()
}

main()
    .catch((e) => {
        throw e
    })
    .finally(async () => {
        console.log(`Finished script.`)
    })

Running this using node --loader ts-paths-esm-loader gives::

(node:1090564) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ReferenceError: exports is not defined in ES module scope
    at file:///home/wvhulle/Documents/Inbiose/dsp/src/lib/ionExchange/script.ts:26:23
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

This error was not there a week ago.

luanglopes commented 1 year ago

Hello @wvhulle, could you give more detail, like your package.json, tsconfig, build and start scripts?

wvhulle commented 1 year ago

Yes, thank you for the speedy response. This is my tsconfig:

{
    "ts-node": {
            "experimentalSpecifierResolution" :"node",
            "esm": true,
    },
    "compilerOptions": {
        "allowSyntheticDefaultImports" : true,
        "moduleResolution": "node",
        "module": "es2020",
        "lib": [
            "es2020",
            "DOM"
        ],
        "target": "es2020",
        /**
            svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
            to enforce using \`import type\` instead of \`import\` for Types.
            */
        "importsNotUsedAsValues": "error",
        "resolveJsonModule": true,
        /**
            To have warnings/errors of the Svelte compiler at the correct position,
            enable source maps by default.
            */  
        "sourceMap": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "baseUrl": ".",
        "allowJs": false,
        "checkJs": false,
        "paths": {
            "$lib": [
                "../src/lib"
            ],
            "$lib/*": [
                "../src/lib/*"
            ]
        },
    },
    "include": [
        "**/*.ts",
        "**/*.d.ts",
        "**/*.js"
    ],
    "exclude": [
        "../node_modules",
    ]
}

My package.json:

  "name": "dsp",
  "version": "7.0.0",
  "main": "svelte.config.js",
  "directories": {
    "doc": "docs",
    "test": "tests"
  },
  "dependencies": {
    "@neodrag/svelte": "^1.2.4",
    "@prisma/client": "^4.8.0",
    "carbon-icons-svelte": "^11.4.0",
    "csv": "^6.2.1",
    "dotenv": "^16.0.3",
    "fuzzysort": "^2.0.1",
    "ldap-authentication": "^2.3.1",
    "npm-check-updates": "^16.0.5",
    "pg": "^8.8.0",
    "snapsvg": "^0.5.1",
    "svelte-time": "^0.7.1",
    "xlsx": "https://cdn.sheetjs.com/xlsx-0.19.0/xlsx-0.19.0.tgz",
    "zod": "^3.19.1"
  },
  "devDependencies": {
    "@faker-js/faker": "^7.4.0",
    "@fingerprintjs/fingerprintjs": "^3.3.6",
    "@playwright/experimental-ct-svelte": "^1.25.1",
    "@playwright/test": "^1.25.0",
    "@sveltejs/adapter-node": "^1.0.0",
    "@sveltejs/kit": "1.0.1",
    "@tailwindcss/typography": "^0.5.4",
    "@types/express": "^4.17.14",
    "@types/lodash": "^4.14.184",
    "@types/node": "^18.11.9",
    "@types/pg": "^8.6.6",
    "@typescript-eslint/eslint-plugin": "^5.34.0",
    "@typescript-eslint/parser": "^5.34.0",
    "@untemps/svelte-use-drop-outside": "^1.6.0",
    "@vitest/coverage-c8": "^0.23.1",
    "autoprefixer": "^10.4.8",
    "carbon-preprocess-svelte": "^0.9.1",
    "chart.js": "^3.9.1",
    "concurrently": "^7.5.0",
    "csv-parse": "^5.3.0",
    "cuid": "^2.1.8",
    "d3": "^7.6.1",
    "d3-scale": "^4.0.2",
    "d3-shape": "^3.1.0",
    "daisyui": "^2.24.0",
    "date-picker-svelte": "^2.2.4",
    "dayjs": "^1.11.5",
    "eslint": "^8.22.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-svelte3": "^4.0.0",
    "express": "^4.18.2",
    "fast-check": "^3.1.2",
    "flubber": "^0.4.2",
    "jsdom": "^20.0.0",
    "jshint": "^2.13.5",
    "katex": "^0.16.0",
    "lodash": "^4.17.21",
    "node-forge": "^1.3.1",
    "papaparse": "^5.3.2",
    "postcss": "^8.4.16",
    "postcss-load-config": "^4.0.1",
    "prettier": "^2.7.1",
    "prettier-plugin-svelte": "^2.7.0",
    "prisma": "^4.8.0",
    "socket.io": "^4.5.3",
    "socket.io-client": "^4.5.3",
    "sortablejs": "^1.15.0",
    "svelte": "^3.54.0",
    "svelte-check": "^2.8.1",
    "svelte-preprocess": "^4.10.7",
    "svelte-select": "^4.4.7",
    "tailwindcss": "^3.1.8",
    "ts-node": "^10.9.1",
    "ts-paths-esm-loader": "^1.3.2",
    "tslib": "^2.4.1",
    "typescript": "^4.8.4",
    "vite": "^4.0.0",
    "vitest": "^0.25.3"
  },
  "scripts": {
    "dev": "eval $(grep '^PUBLIC_PORT' .env) && vite dev --port $PUBLIC_PORT",
    "build": "vite build",
    "package": "svelte-kit package",
    "preview": "eval $(grep '^TEST_PORT' .env) && vite preview --port $TEST_PORT",
    "check": "svelte-check --tsconfig ./tsconfig.json",
    "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
    "lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
    "format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. .",
    "test-e2e": "playwright test",
    "deploy": "bash deploy.sh",
    "test": "vitest",
    "coverage": "vitest run --coverage"
  },

  "type": "module",

}
wvhulle commented 1 year ago

The tsconfig i shared is for a subproject of the main project that modifies a sveltekit server to support websockets. I didn't run that server however to arrive at the error output mentioned in my first post.

luanglopes commented 1 year ago

I created a small project to try to reproduce the error you got, please download it and try on your own machine, it worked for me, so if it works for you, probably it is not related to this package.

I ran node --loader ts-paths-esm-loader src/index.ts

https://stackblitz.com/edit/node-tzu7um?file=package.json

wvhulle commented 1 year ago

I see, i was not passing the right tsconfig. Is there no option to pass the tsconfig file as an option in this repository?

luanglopes commented 1 year ago

Try running it like this node --loader ts-paths-esm-loader src/index.ts --project ./path/to/tsconfig.json ts-node uses --project arg to get the config file

luanglopes commented 1 year ago

@wvhulle did it fixed the problem for you?

wvhulle commented 1 year ago

Hey I've tested that out but it didn't work.

luanglopes commented 1 year ago

Ok, so maybe the only way is to run the command from a directory that has a tsconfig.json with the right config