Skip to content

Commit

Permalink
Require Node.js 12
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
sindresorhus committed Jun 1, 2021
1 parent 30753e5 commit f8e7fb6
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 27 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions 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 {}
};
2 changes: 1 addition & 1 deletion license
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:

Expand Down
8 changes: 3 additions & 5 deletions package.json
Expand Up @@ -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"
Expand All @@ -29,9 +30,6 @@
"import",
"path"
],
"dependencies": {
"resolve-from": "^5.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
Expand Down
10 changes: 0 additions & 10 deletions readme.md
Expand Up @@ -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
Expand All @@ -20,7 +18,6 @@ const importFrom = require('import-from');
importFrom('foo', './bar');
```


## API

### importFrom(fromDirectory, moduleId)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion test.js
Expand Up @@ -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'/);
});
Expand Down

0 comments on commit f8e7fb6

Please sign in to comment.