Coder Social home page Coder Social logo

folke / esbuild-runner Goto Github PK

View Code? Open in Web Editor NEW
701.0 3.0 26.0 420 KB

⚡️ Super-fast on-the-fly transpilation of modern JS, TypeScript and JSX using esbuild

Home Page: https://www.npmjs.com/package/esbuild-runner

License: Apache License 2.0

JavaScript 32.21% TypeScript 67.25% Shell 0.53%
nodejs node esbuild esbuild-node typescript esnext jest tsnode ts-node esbuild-runner

esbuild-runner's Introduction

Hi there 🥷🏻

Folke's github stats

Top Langs

esbuild-runner's People

Contributors

folke avatar jacob-lockwood avatar mmamedel avatar renovate-bot avatar renovate[bot] avatar teintinu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

esbuild-runner's Issues

Configuration option loader(s) is ignored

Hi,

in the same vein as #23, unless I'm mistaken, it seems that the --loader esbuild option is not currently supported.

Similarly to the externals issue, a merge of user provided loaders with the default stack could be considered.

My use case is testing modules that use static binary assets.

My esbuild-runner-config.js:

module.exports = {
  type: 'bundle',
  esbuild: {
    target: 'node14',
    format: 'cjs',
    loader: {
      '.png': 'binary'
    }
  }
}

Result:

 > lib/loader-issue.js:1:16: error: No loader is configured for ".png" files: assets/img/image.png
    1 │ import pngImage from '../assets/img/image.png'
      ╵                      ~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks for a great tool!

Cannot use import statement outside a module even when targeting CommonJS

I'm trying to switch from ts-node to esbuild-runner for my package https://github.com/Julien-Marcou/static-html

Here is the problematic file bin/static.ts :

#!/usr/bin/env -S npx ts-node-script
import * as fs from 'fs';

const usage = 'Usage : npx static <init|build|watch|serve|deploy|clean>\n';

// ...

The goal of this file is to expose npx static commands to developers who install my package.

But when I replace #!/usr/bin/env -S npx ts-node-script by #!/usr/bin/env -S npx esbuild-runner, I then get this error when calling my npx static command :

(node:1888) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/jmarcou/dev/my-project/node_modules/static-html/bin/static.ts:2
import * as fs from 'fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module

As if esbuild thinks I'm targeting ECMAScript Module instead of CommonJS.

I did create a esbuild-runner-config.js file, and tried to put it inside the root folder of the static-html package aswell as the bin folder, but nothing changed.

module.exports = {
  type: 'bundle',
  esbuild: {
    target: 'node14',
    format: 'cjs',
  },
};

Am I doing something wrong ?

Benchmarks?

I’m curious if there are benchmarks between this and say native ESM loading. Just to get a rough idea of cost (cached/cold).

Thanks!

Support setting the working directory

I'm executing a script like this: esr scripts/script

My environment requires the current working directory to be the root of my project. When executing a script inside the scripts folder, the current working directory will not be the root.

Is there any way around this?

Thanks in advance!

ERR_UNKNOWN_FILE_EXTENSION running a basic ts file?

Not sure what I'm doing wrong but...

node -r esbuild-runner/register helloworld.ts 
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/cjr/dev/packages/api/helloworld.ts
    at new NodeError (node:internal/errors:363:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at Loader.import (node:internal/modules/esm/loader:177:17)
    at Object.loadESM (node:internal/process/esm_loader:68:5)

helloworld.ts:

console.log("HELLO WORLD LITERALLY!");
export default {}
node -v
> v16.3.0

Duplicate import in monorepo

In a monorepo, importing from different files of a sibling workspace package leads to duplicate code in generated bundle.

Consider this monorepo:

  • packages/a/1.ts imports ./shared
  • packages/a/2.ts imports ./shared
  • packages/a/shared.ts — this import gets duplicated in esbuild-runner bundle
  • packages/b/index.ts — imports @test/a/1 and @test/a/2

then run:

cd packages/b
pnpm esr index.ts

The code in shared.ts will execute twice.

Complete minimal reproduction: https://github.com/IlyaSemenov/esbuild-runner-monorepo-duplicate-import

Suggested workaround

I believe here:

externals = [
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.devDependencies ?? {}),
]

we can filter externals with something like:

.filter(e => !(pkg.dependencies?.[e] || pkg.devDependencies?.[e]).startsWith("workspace:"))

add support for esbuild options

maybe add support for passing esbuild options for 'install' functions? like this:

const { install } = require('esbuild-runner');

install({
  type: 'transform',
  debug: false,
  esbuild: {
    target: 'esnext',
    plugins: [...]
  }
});

tslib required?

I have a build.ts which I run to compile my source files with esbuild. To run it I have a npm-run-script:

"compile": "node -r esbuild-runner/register build.ts",

We do not have tslib explicitly installed in our project. When running the above command we get the following error:

/Users/<project-folder>/node_modules/esbuild-runner/lib/esbuild.js:22
    externals = (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], Object.keys((_a = pkg.dependencies) !== null && _a !== void 0 ? _a : {}), true), Object.keys((_b = pkg.devDependencies) !== null && _b !== void 0 ? _b : {}), true);
 

TypeError: (0 , tslib_1.__spreadArray) is not a function
    at Object.<anonymous> (/Users/<project-folder>/node_modules/esbuild-runner/lib/esbuild.js:22:70)

20211116-194635_Screenshot

If I add tslib explicitly to the project, the above command runs without any problems.

esbuild.config.js is too generic naming

I believe esbuild.config.js is too generic naming. It misleads the reader as if that were esbuild config, which it is not. It also 'squats' the place for the actual esbuild config, for example if developer wanted to keep and reuse esbuild settings between different components of the project (such as: esbuild-runner, estrella, vite, esbuild-loader, etc.)

I believe esbuild-runner.config.js would be better naming, directly explaining the purpose of the file.

`jest.mock()` calls not getting hoisted to the top by `esbuild-runner/jest`

I created an issue/question for this in SST at sst/sst#1065. I'm wondering if I'm doing something wrong or if this just a shortcoming of esbuild-runner. How come the jest.mock() calls are not hoisted to the top in the generated JavaScript files of Jest tests written in TypeScript? Below is snippet of the resulting JavaScript, notice the jest.mock() calls appear towards the middle, nowhere near the top as Jest babel-jest transformer for example would do,

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const lambda = __importStar(require("../src/acme-lambda"));
jest.mock('ioredis');
jest.mock('@acme/common-util');
...

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency eslint-plugin-node with eslint-plugin-n 14.0.0
  • chore(deps): update dependency husky to v9
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/node.js.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • source-map-support 0.5.21
  • tslib 2.4.0
  • @release-it/conventional-changelog 5.1.1
  • @types/eslint 8.4.7
  • @types/eslint-plugin-prettier 3.1.0
  • @types/node 18.8.5
  • @types/prettier 2.7.1
  • @types/rimraf 3.0.2
  • @types/source-map-support 0.5.6
  • @typescript-eslint/eslint-plugin 5.40.1
  • @typescript-eslint/parser 5.40.1
  • devmoji 2.3.0
  • esbuild 0.15.12
  • eslint 8.25.0
  • eslint-config-prettier 8.5.0
  • eslint-plugin-eslint-comments 3.2.0
  • eslint-plugin-import 2.26.0
  • eslint-plugin-jest 27.1.3
  • eslint-plugin-node 11.1.0
  • eslint-plugin-prettier 4.2.1
  • eslint-plugin-promise 6.1.1
  • eslint-plugin-unicorn 44.0.2
  • husky 8.0.1
  • jest 29.2.1
  • prettier 2.7.1
  • release-it 15.5.0
  • rimraf 3.0.2
  • type-fest 3.1.0
  • typescript 4.8.4
  • typesync 0.9.2
  • esbuild *

  • Check this box to trigger a request for Renovate to run again on this repository

Working with mocha and nyc

Hi,

I love esbuild-runner, however I tried to replace my toolchain for testing with esbuild-runner but I can't get the same result as ts-node did.

I ran nyc with mocha like this,

nyc mocha -r ts-node/register './src/**/*.test.ts' --extension ts

and replace with

nyc mocha -r esbuild-runner/register './src/**/*.test.ts' --extension ts

But it seemed I didn't get the same result as ts-node version

Thanks

esbuild plugins not work

config

// esbuild-runner.config.js
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const { esbuildDecorators } = require('@anatine/esbuild-decorators');

const tsconfig = path.resolve(__dirname, './tsconfig.json');
const getEsbuildOptions = async () => ({
    // Any esbuild build or transform options go here
    platform: 'node',
    target: 'esnext',
    sourcemap: false,
    plugins: [esbuildDecorators({ tsconfig, cwd: process.cwd() })],
});
const getConfig = async () => ({
    type: 'bundle', // bundle or transform (see description above)
    esbuild: await getEsbuildOptions(),
});
module.exports = getConfig();

bin.js

if (typeof process.env.NODE_ENV === 'undefined') {
    process.env.NODE_ENV = 'development';
}
require('esbuild-runner/register');
require('module-alias/register');

error

ColumnTypeUndefinedError: Column type for PostEntity#title is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.
    at new Co

Sibling workspace module's node_modules should not be transpiled

The documentation says:

Dependencies defined in package.json or node_modules will never be transpiled.

This is not entirely true. In a pnpm project with multiple workspaces, node_modules coming from a sibling workspace module are transpiled. I believe they should not, similar to the module's own node_modules.

typescript check before run

any plugin or way check type error before run?
I have wrong type in typescript but esbuild runner just run code without checking type.
I install npm i esbuild-plugin-ts-checker and add to plugin but not work

Add `-v` or `--version` option to print version of esbuild-runner

Is your feature request related to a problem? Please describe.

I often use esbuild-runner CLI for running test code written in TypeScript. It's helpful to print esr version while sharing the test data and output for easy reproduction. However, esr does not have -v or --version option.

$ node -v
v16.18.1

$ esr -v
Usage: esr [options] <source-file> [file-options]

  --cache       Transform on a file per file basis and cache code
  --clearCache  Clear transform cache
  --help|-h     Display this help message
  
$ esr --version
Usage: esr [options] <source-file> [file-options]

  --cache       Transform on a file per file basis and cache code
  --clearCache  Clear transform cache
  --help|-h     Display this help message

Describe the solution you'd like

Add -v or --version option which prints version of the esbuild-runner being used.

Describe alternatives you've considered

I use fnm for managing node versions. I can run grep on package.json file

$ grep '"version"' /run/user/267807/fnm_multishells/22654_1669997929042/lib/node_modules/esbuild-runner/package.json
  "version": "2.2.2",

However, the package.json will be located on different path for nvm, or node installed directly.

JSX syntax extension is not currently enabled for Jest runner

Problem

It seems like the loader I defined in my config is not being passed to Esbuild.

esbuild-runner.config.js

module.exports = {
  debug: true,
  esbuild: {
    loader: { '.js': 'jsx' },
    target: 'esnext',
  },
  type: 'transform', // I also tried bundle
};

Note: Dropped a console.log in this file, it is being loaded.

Jest config

{
  "transform": {
    "^.+\\.(js|jsx|ts|tsx)$": "esbuild-runner/jest"
  }
}

Error Message

yarn run v1.22.17
$ jest spec/javascript/components/Accordion.test.js

 RUNS  spec/javascript/components/Accordion.test.js
✘ [ERROR] The JSX syntax extension is not currently enabled

    /workspace/spec/javascript/components/Accordion.test.js:11:6:
      11 │       <Accordion expanded={false}>
         ╵       ^

  The esbuild loader for this file is currently set to "js" but it must be set to "jsx" to
 FAIL  spec/javascript/components/Accordion.test.js { '.js': 'jsx' }" to do that.
  ● Test suite failed to run

    Transform failed with 1 error:
    /workspace/spec/javascript/components/Accordion.test.js:11:6: ERROR: The JSX syntax extension is not currently enabled

      at failureErrorWithLog (node_modules/esbuild/lib/main.js:1603:15)
      at node_modules/esbuild/lib/main.js:1392:29
      at node_modules/esbuild/lib/main.js:666:9
      at handleIncomingPacket (node_modules/esbuild/lib/main.js:763:9)
      at Socket.readFromStdout (node_modules/esbuild/lib/main.js:632:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.228 s
Ran all test suites matching /spec\/javascript\/components\/Accordion.test.js/i.
error Command failed with exit code 1.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Configuration option external is ignored

Hello,

I tried defining pg-native as an external dependency but it seems the option is ignored, i.e., not being merged with the computed externals here:

external: externals,

My esbuild-runner.config.js:

module.exports = {
  type: 'bundle',
  esbuild: {
    external: ['pg-native'],
  },
}

and the error that is produced when running:

 > ../../node_modules/pg/lib/native/client.js:4:21: error: Could not resolve "pg-native" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    4 │ var Native = require('pg-native')
      ╵                      ~~~~~~~~~~~


/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1466
  let error = new Error(`${text}${summary}`);
              ^
Error: Build failed with 1 error:
../../node_modules/pg/lib/native/client.js:4:21: error: Could not resolve "pg-native" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    at failureErrorWithLog (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1466:15)
    at /Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1148:28
    at runOnEndCallbacks (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1066:65)
    at buildResponseToResult (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1146:7)
    at /Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1253:14
    at /Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:726:9)
    at readFromStdout (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:596:7)
    at runServiceSync (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1831:3)
    at Object.buildSync (/Users/vieira/Code/f1/node_modules/esbuild/lib/main.js:1651:3)

I should note that I am using npm7 workspaces and that pg-native is a dependency of a dependency of another package in the workspace, if I add the dependency directly to the package.json of the package I am trying to run it works (the externals computation finds it).

Question / Request for documentation examples

I am guessing that many, like myself, would like to use esbuild-runner in their Typescript tool chain.

It would be very useful for people who want to try things out to have working examples with different links in the chain other than Jest. For examples might include how to use with:

  • mocha
  • node 18's test runner
  • nyc
  • c8

I would be more than happy to help with this but my attempts to get it to work with node --test -r esbuild-runner/register have failed...

Configuration option loader is ignored when type: "transform"

Hi,

I noticed having type: 'bundle' in my esbuild-runner config broke my test coverage, so swapped it out to "transform".

This, however, caused my loader definition to be ignored.

Would it be possible to give type: 'transform' the same love you gave to bundle in #24 ?

Thanks!

Watch mode

Loving this tools. Makes using esbuild that bit easier.

One thing I would love, is a first class support for watching. I currently use nodemon to accomplish this, which works reasonably well, but wonder if this could be improved, by having esbuild runner do it all (and keeping it in memory):

yarn run nodemon --watch 'src/**/*' -e ts,tsx --exec 'dotenv -e=.env -- yarn run esr --cache src/main.ts'"

passing pre-load packages like nodejs

Hi,
trying to replace node with esr, here is the nodejs way in package.json:

"d": " node -r dotenv/config --inspect=0.0.0.0",

how can we achieve this? following not working:

 "esr": " esr -r dotenv/config --inspect=0.0.0.0",

paths in `Error.stack` "multiply" ...?

I tried switching from ts-node to esbuild-runner, as you can see here.

In this project, and I obtain paths from Error.stack, very simply, as you can see here:

        const location = new Error().stack!
          .split("\n")[2]
          .replace(/^\s+at /, "");

For some reason, the paths in stack-traces appear to... multiply?

For example, if you run npm run test, you'll see test output like this:

  × [equal] it produces the expected test results
  └ /home/mindplay/workspace/funky-test/test/test/test.ts:76:10

That path is /home/mindplay/workspace/funky-test/test/test.ts:76:10, so there's a /test too many in there.

As there is test coverage for that fact, you will see failing tests as well:

   ×          "location": "/home/mindplay/workspace/funky-test/test/test/cases.ts:9:6",
   √          "location": "/home/mindplay/workspace/funky-test/test/cases.ts:9:6",

Something appears to be not right with the run-time path resolution of this loader?

Fails in jest 28

In jest version 28 was changed transform return type
https://jestjs.io/docs/upgrading-to-jest28#transformer

I used this patch to bypass the problem

index df993a4..be2603e 100644
--- a/node_modules/esbuild-runner/lib/jest.js
+++ b/node_modules/esbuild-runner/lib/jest.js
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
 var esbuild_1 = require("./esbuild");
 require("./register");
 function process(src, filename) {
-    return (0, esbuild_1.transpile)(src, filename, { type: "transform" });
+    const code = (0, esbuild_1.transpile)(src, filename, { type: 'transform' })
+    return { code }
 }
 exports.default = { process: process };
 //# sourceMappingURL=jest.js.map

New version release

Thanks for a great library!

Can we bother you for a new version release? It'd be nice to get Jest v28 support #65 out so we can upgrade our dependencies.

Allow package.json dependencies to be selectively transpiled / bundled

The esbuild-node-externals plugin allows you to specify an allowList parameter for including dependencies into the bundle.

Why would you want this? For example:

  • A project (typically a monorepo) that uses internal dependencies of type "foo": "link:.src/some/path/foo" in the package.json. These aren't really external dependencies and they should be bundled/transpiled, they're just a convenient way to make bits of the source appear as top-level packages for import.
  • Creating single-file deployments, where the entire node_modules is intentionally bundled. It's possible and convenient if you have no native binary dependencies.
  • Bundling only selected (perhaps vendored) node_modules packages.

In our case, the first bullet prevents us from using esbuild-runner for some of our tools: our source tree is pure ESM, esr transpiles to CJS for running under node except the internally linked entries defined in packages.json aren't being bundled, so the result doesn't run.

I would like to suggest one or two options:

  • Add an esr config option allowList or externalsAllowList, specifying dependencies that should be transpiled/bundled.
  • Add an esr config option to disable the default externalizing of node_modules entirely. Then I could presumably add the esbuild-node-externals plugin myself to the config, or write my own plugin like in the esbuild docs, which supports fine-grained control of which dependencies to include or exclude.

If this is something you'd consider, I could try to make a PR.

Incompatible with node-tap sourcemap support

Problem

When I use node-tap with esbuild-runner, it fails to parse source code and show proper line numbers. When I replace esbuild-runner with @swc-node/register, everything works fine.

I am not exactly sure it's esbuild-runner's problem, but I suspect so. I don't think node-tap has some inner hacks specifically tailored to @swc-node/register, it's more probable that esbuild-runner fails to emit the source map in the generally assumable way.

Example

Example source code:

import tap from "tap"

tap.same("a", "b")

Test with @swc-node/register

Everything works as expected, it shows source code fully:

yarn tap --node-arg '-r' --node-arg '@swc-node/register' test.ts

emits:

 FAIL  test.ts
 ✖ should be equivalent

  test.ts
  1 | import tap from "tap"
  2 |
> 3 | tap.same("a", "b")
    | ----^

  --- expected
  +++ actual
  @@ -1,1 +1,1 @@
  -"b"
  +"a"

Test with esbuild-register

It fails to parse sourcemap and show source code with proper line numbers:

yarn tap --node-arg '-r' --node-arg 'esbuild-register' test.ts

emits:

 FAIL  test.ts
 ✖ should be equivalent

  --- expected
  +++ actual
  @@ -1,1 +1,1 @@
  -"b"
  +"a"

  test: test.ts
  at:
    line: 20
    column: 20
    file: test.ts
  stack: |
    Object.<anonymous> (test.ts:20:20)
    Module.replacementCompile (node_modules/append-transform/index.js:60:13)
    Module._compile (node_modules/esbuild-register/dist/node.js:2258:26)
    module.exports (node_modules/default-require-extensions/js.js:7:9)
    node_modules/append-transform/index.js:64:4
    newLoader (node_modules/esbuild-register/dist/node.js:2262:9)
    Object.<anonymous> (node_modules/append-transform/index.js:64:4)

Reproduction repository

See https://github.com/IlyaSemenov/esbuild-runner-node-tap-repro for the complete repro.

`import.meta` not available in Jest

Thanks for the package, it performs well and is simple to set up. ❤️

I'm using it as a Jest transformer and trying to test a function written in Typescript to read a package JSON file:

import { createRequire } from 'module'
import { resolve } from 'path'

export const requirePackage = (path: string) =>
  createRequire(import.meta.url)(resolve(path, 'package.json'))

I get an error from import.meta.url being undefined.

Possibly related issue in TS-Jest? kulshekhar/ts-jest#1174

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.