Home > Software design >  How to simply import in Node? (Vanilla JS version) - including type:module
How to simply import in Node? (Vanilla JS version) - including type:module

Time:01-06

This is a JS version of similar (unresolved) TS question. See: How to simply import in Node? (Typescript version)

Trying the simplest import in Node, but am not being able to get it to work....

Note!!! Added "type": "module" as suggested in Node.js - SyntaxError: Unexpected token import

package.json

{
  "type": "module",
  "name": "import_js",
  "version": "1.0.0",
  "description": "dummy project",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

index.js

import foo from './foo';
foo();

foo.js

export default function foo() {
  console.log("FOO");
}

result

$ node index.js
/Users/projects/sb/import_js/index.js:1
import foo from './foo';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._compile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

CodePudding user response:

After recreating your same three files in a directory by themselves I found that if I change this:

import foo from './foo';

to this:

import foo from './foo.js';

Then, it works. I believe I remember something about ESM modules that says you have to specify the file extension and you can't just use the root name like you can with CommonJS modules.

Using your exact code, recreated in the same three files, running node v16.13.0, making this one change makes it work for me.


Now, I did not originally get the same error you got. I got this error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module xxx import from yyy

I wonder if you had a file named just foo somehow that it was finding? If not, something else is still different in your system than mine.

I wonder if you're still trying to use a TypeScript compiler and the TypeScript compile target is set for CommonJS modules?

Because the error you get is somehow indicating that a CommonJS module is getting loaded, but there is no reason for that if given only these three files in a directory by themselves and no compile step of any kind.

Things for you to check.

  1. Are you sure that when you run node, you're running the plain node executable and not something that Typescript has installed that will try to compile the sources using some default configuration?

  2. Are there ANY other files in this directory? If so, then isolate these three files into their own directory where there are no other package.json files anywhere in the parent tree.

  3. Try executing the test.js file from the same directory (so using the same package.json) below and see what it outputs:

Sample file:

// test.js
let foundDirname = typeof __dirname === "string";
let foundModule = typeof module === "object";

console.log("found __dirname", foundDirname);
console.log("found module", foundModule);

if (foundDirname && foundModule) {
    console.log("appears to be a CommonJS module")
} else {
    console.log("appears to be an ESM module");
}

This should tell you whether it is treating your file as a CommonJS module or an ESM module.

Run this test file with:

node test.js

CodePudding user response:

Simply import like this.

const foo = require("./foo.js");
  •  Tags:  
  • Related