From f8e7fb6671bb7ed3165acca51a93e59e2b7affe4 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 1 Jun 2021 13:45:03 +0700 Subject: [PATCH] Require Node.js 12 Fixes #5 --- .github/funding.yml | 3 --- .github/workflows/main.yml | 5 ++--- index.js | 9 +++++---- license | 2 +- package.json | 8 +++----- readme.md | 10 ---------- test.js | 2 +- 7 files changed, 12 insertions(+), 27 deletions(-) delete mode 100644 .github/funding.yml diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1a630e9..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,3 +0,0 @@ -github: sindresorhus -open_collective: sindresorhus -custom: https://sindresorhus.com/donate diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,13 +10,12 @@ jobs: fail-fast: false matrix: node-version: + - 16 - 14 - 12 - - 10 - - 8 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.js b/index.js index cc39b35..dd6f71e 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,11 @@ 'use strict'; -const resolveFrom = require('resolve-from'); +const path = require('path'); +const {createRequire} = require('module'); -module.exports = (fromDirectory, moduleId) => require(resolveFrom(fromDirectory, moduleId)); +module.exports = (fromDirectory, moduleId) => createRequire(path.resolve(fromDirectory, 'noop.js'))(moduleId); module.exports.silent = (fromDirectory, moduleId) => { try { - return require(resolveFrom(fromDirectory, moduleId)); - } catch (_) {} + return createRequire(path.resolve(fromDirectory, 'noop.js'))(moduleId); + } catch {} }; diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 511f842..7bf989f 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,14 @@ "description": "Import a module like with `require()` but from a given path", "license": "MIT", "repository": "sindresorhus/import-from", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "engines": { - "node": ">=8" + "node": ">=12.2" }, "scripts": { "test": "xo && ava && tsd" @@ -29,9 +30,6 @@ "import", "path" ], - "dependencies": { - "resolve-from": "^5.0.0" - }, "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.2", diff --git a/readme.md b/readme.md index a2de09b..777fce5 100644 --- a/readme.md +++ b/readme.md @@ -2,14 +2,12 @@ > Import a module like with [`require()`](https://nodejs.org/api/modules.html#modules_require_id) but from a given path - ## Install ``` $ npm install import-from ``` - ## Usage ```js @@ -20,7 +18,6 @@ const importFrom = require('import-from'); importFrom('foo', './bar'); ``` - ## API ### importFrom(fromDirectory, moduleId) @@ -43,7 +40,6 @@ Type: `string` What you would use in `require()`. - ## Tip Create a partial using a bound function if you want to import from the same `fromDir` multiple times: @@ -55,7 +51,6 @@ importFromFoo('./bar'); importFromFoo('./baz'); ``` - ## Related - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory @@ -64,8 +59,3 @@ importFromFoo('./baz'); - [resolve-pkg](https://github.com/sindresorhus/resolve-pkg) - Resolve the path of a package regardless of it having an entry point - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily - [import-global](https://github.com/sindresorhus/import-global) - Import a globally installed module - - -## License - -MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index 36d7da0..d442706 100644 --- a/test.js +++ b/test.js @@ -7,7 +7,7 @@ test('importFrom()', t => { const moduleNotFoundError = t.throws(() => { importFrom('fixture', './nonexistent'); - }, Error); + }); t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND'); t.regex(moduleNotFoundError.message, /^Cannot find module '.\/nonexistent'/); });