13

Let's say my app looks like this

├── index.js
└── src
    ├── do_foo.js

do_foo.js

function foo() {
    return "bar";
}

export default foo;

index.js

import foo from 'src/do_foo';

foo();

Running node index.js results in the following error

> node index.js

internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'src' imported from /mnt/c/Users/*******/Projects/*******/index.js
    at packageResolve (internal/modules/esm/resolve.js:620:9)
    at moduleResolve (internal/modules/esm/resolve.js:659:14)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:752:11)
    at Loader.resolve (internal/modules/esm/loader.js:97:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:242:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:50:40)
    at link (internal/modules/esm/module_job.js:49:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the *******@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /mnt/c/Users/*******/.npm/_logs/2020-07-21T03_38_34_404Z-debug.log

Seeing as I've managed to mess up at such an early point, the solution, I'd imagine, has to be pretty simple. What cardinal rule of javascript am I violating here?

I should add that I know everything will work if everything is in the same folder, but that's not what I'm aiming for. If at all possible, it's a better look to keep index.js in the outermost directory.

Edit: I want to clear up that my problem was NOT caused by a typo. My code was syntactically fine, it was the import that needed a relative scope. I mistyped something when I was writing the example code, that had nothing to do with the initial question.

6
  • 1
    1) Get your quotes right. 2) Use a relative path, ie import foo from './src/do_foo'
    – Phil
    Jul 21, 2020 at 3:58
  • oh i typed everything from scratch to make it minimally reproducible - must have been a typo @Phil
    – notacorn
    Jul 21, 2020 at 3:59
  • you answered my question, the one you linked is about making imports readable and they never ran into any kind of error @Phil
    – notacorn
    Jul 21, 2020 at 4:08
  • 1
    True. I'm sure there's an applicable duplicate out there somewhere though 😇
    – Phil
    Jul 21, 2020 at 4:10
  • im still happy to give you credit if you post an answer @Phil
    – notacorn
    Jul 21, 2020 at 4:10

3 Answers 3

16

You must use the full file path to get this working.

import foo from './src/do_foo.js';

console.log(foo());

Since its an experimental feature, you will get this warning.

ExperimentalWarning: The ESM module loader is experimental.
3
  • 1
    i dont get any kind of warning
    – notacorn
    Jul 21, 2020 at 4:14
  • 1
    What about when you hit this in a monorepo...?
    – Slbox
    Jun 1, 2021 at 19:10
  • As of node 14 esm module support in node is now stable. Jan 4, 2022 at 22:30
2

Yes, you are using typescript, in the tsconfig change "module":"esnext" for "module":"commonjs" and in the package.json "module":"commonjs".

0

This is is another way, albeit basic.

In your foo.js

You can run module.exports = foo;

In your index.js you can run

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 3000;

app.set('view engine', 'ejs');

app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));

const foo = require(__dirname + "/foo.js");

foo();

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.