Edmond Tamas Edmond Tamas - 1 month ago 15
Node.js Question

Meteor Shell, can't import files from any folder: Error: Cannot find module ‘/imports/api/donuts/collection.js’

I am having some trouble importing modules into the meteor shell.

Simple example:

1.create new project (meteor create myproject)

2.create file /imports/api/donuts/collection.js and paste content:

// file: /imports/api/donuts/collection.js
import { Mongo } from 'meteor/mongo';
const Donuts = new Mongo.Collection('donuts');
export default Donuts;


3.Run meteor shell and import the file by:

import Donuts from '/imports/api/donuts/collection.js'


than this error hits up:

Error: Cannot find module '/imports/api/donuts/collection.js'
at Function.require.resolve (packages/modules-runtime.js:129:19)
at Module.resolve (packages/modules-runtime.js:81:25)
at Module.Mp.import (/home/ec2-user/.meteor/packages/modules/.0.7.7.mccaq7++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:61:29)
at repl:1:-37
at packages/shell-server/shell-server.js:458:25
at /home/ec2-user/.meteor/packages/promise/.0.8.8.i94065++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:32:39


What's wrong? File permissions are ok, I start the meteor shell from project root.

Thanks!

Answer

Meteor originally loaded all of the source files using its default load order.

In more recent versions (circa v1.3), it treats special directories differently. One of those directories is imports.

Any directory named imports/ is not loaded anywhere and files must be imported using import.

(from the Meteor docs)

When using the shell, you can only import resources that were included in the build. If the module (file) you are trying to import is not included in your import tree (chain of imports starting somewhere outside of the /imports directory), it will not be available for import.